我想知道什么时候调用模板类的成员函数。定义在哪里生成?例如:
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
}
所以我想知道模板类在第 1点和第 2A<int>
点的样子
案例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
那么这两种情况中哪一种是正确的呢?