我读到模板copy-con永远不是默认的onstructor,而模板赋值操作永远不是复制赋值操作符.
我无法理解为什么需要这个限制并立即上线到ideone并返回一个测试程序但是这里复制构造函数永远不会被调用进一步的谷歌搜索我遇到了模板化的构造函数并尝试了但仍然从未调用复制构造函数.
#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() …Run Code Online (Sandbox Code Playgroud) 如何为模板类编写复制构造函数.因此,如果模板参数是另一个用户定义的类,那么它的复制构造函数也会被调用.
以下是我的课
template <typename _TyV>
class Vertex {
public:
Vertex(_TyV in) : m_Label(in){ }
~Vertex() { }
bool operator < ( const Vertex & right) const {
return m_Label < right.m_Label;
}
bool operator == ( const Vertex & right ) const {
return m_Label == right.m_Label;
}
friend std::ostream& operator << (std::ostream& os, const Vertex& vertex) {
return os << vertex.m_Label;
}
_TyV getLabel() { return m_Label;}
private:
_TyV m_Label;
public:
VertexColor m_Color;
protected:
};
Run Code Online (Sandbox Code Playgroud)