#include <iostream>
class X { public: template<typename T> X( const T& t) { std::cout << "This is ctor." << std::endl; }
template<typename T> X& operator=( const T& ) { std::cout << "This is =." << std::endl; } };
int main() { int n = 5; X a(n); //
double d = 10.5; X b(d); //
X c = a; c = b; system("pause"); return 0; }
|
类内的构造函数和赋值运算符函数都是模板,但是该模板参数T绝不会实例化为该类型X。也就是说这个构造函数起不到拷贝构造函数的作用,这个赋值运算符函数也起不到常规赋值的作用,但是此时参数实例化为其他非X类型还是可以的。
输出如下所示:
This is ctor. This is ctor. 请按任意键继续. . .
|
阅读(1240) | 评论(0) | 转发(0) |