相关疑难解决方法(0)

为什么未调用的模板类成员未实例化?

我想知道当我通过指定模板类型参数创建类模板的实例时.
1)为什么非被调用的函数没有得到实例化?.
2)在我尝试使用之前,他们不会被编译?
3)这种行为背后的逻辑是什么?

template <class T>
class cat{
public:

T a;
void show(){
   cout << a[0];
}
void hello(){
   cout << "hello() get called \n";
}
}; 

int main(){
cat<int> ob1; // I know that show() did not get instatiated, otherwise I will get an    error since a is an int
ob1.hello();

 }
Run Code Online (Sandbox Code Playgroud)

c++ templates class

5
推荐指数
2
解决办法
232
查看次数

标签 统计

c++ ×1

class ×1

templates ×1