- Expert C++
- Vardan Grigoryan Shunguang Wu
- 105字
- 2025-04-04 13:09:42
Instantiation
Considering the class template, V, we defined in the previous section, we'll assume the following declarations appear later:
V<char> cV;
V<int> iV(10);
V<float> fV(5);
Then, the compiler will create three instances of the V class, as follows:
class V<char>{
public:
V(int n=0);
// ...
public:
int m_nEle;
char *m_buf;
};
class V<int>{
public:
V(int n=0);
// ...
public:
int m_nEle;
int *m_buf;
};
class V<float>{
public:
V(int n = 0);
// ...
public:
int m_nEle;
float *m_buf;
};
Similar to function template instantiation, there are two forms of class template instantiation – explicit instantiation and implicit instantiation. Let's take a look at them.