【发布时间】:2012-08-25 16:09:00
【问题描述】:
我想知道什么时候调用模板类的成员函数。定义在哪里生成?例如:
template <class T>
class A{
public:
A(){cout << "A<T>::A() " << endl;}
void f(){cout << "A<T>::f() " << endl;}
};
int main(){
A<int> ob; // Time t1
ob.f(); // Time t2
}
所以我想知道模板 classA<int> 在 point 1 & point 2
案例1:
时间t1:
class A<int>{
public:
A(){cout << "A<T>::A()" << endl;} // A() body is defined inline
void f(); // becasue I didn't call A<int>::f yet so there is just a declaration
};
时间 t1
class A<int>{
public:
A(){cout << "A<T>::A()" << endl;} // A() body is defined inline
void f(){cout << "A<T>::f()" << endl;} // f() is defined inline
};
案例1:
时间t1
class A<int>{
public:
A();
void f();
};
A<int>::A(){cout << "A<T>::A()" << endl;} // this is not inline
时间 t2
class A<int>{
public:
A();
void f();
};
A<int>::A(){cout << "A<T>::A()" << endl;} // this is not inline
void A<int>::f(){cout << "A<T>::f() " << endl;}// this is not inline
那么这两种情况哪一种是正确的?
【问题讨论】:
-
我认为所有的魔法都发生在时间 t1。模板 A
被定义。
标签: c++ templates instantiation