【发布时间】:2013-10-03 19:04:59
【问题描述】:
我读到模板 copy-con 永远不是默认复制 onstructor,模板 assignment-op 永远不是复制分配运算符。
我不明白为什么需要这个限制并立即上网到 ideone 并返回 test program 但这里复制构造函数永远不会被调用进一步谷歌搜索我遇到了模板化构造函数并尝试了但它仍然从不调用复制构造函数。
#include <iostream>
using namespace std;
template <typename T> class tt
{
public :
tt()
{
std::cout << std::endl << " CONSTRUCTOR" << std::endl;
}
template <typename U> const tt<T>& operator=(const tt<U>& that){std::cout << std::endl << " OPERATOR" << std::endl;}
template <typename U> tt(const tt<U>& that)
{
std::cout << std::endl << " COPY CONSTRUCTOR" << std::endl;
}
};
tt<int> test(void)
{
std::cout << std::endl << " INSIDE " << std::endl; tt<int> a; return a;
}
int main() {
// your code goes here
tt<int> a ; a = test();
return 0;
}
谁能解释一下设置这个限制的全部原因以及如何编写模板类的复制构造函数。
谢谢
【问题讨论】:
-
没有“默认复制构造函数”。只有 一个 复制构造函数。模板绝不是它。
-
我编辑了代码并在 ideone 中运行它,但仍然没有调用复制构造函数。但我也无法理解这种限制背后的原因
标签: c++