如何编写Template类复制构造函数

Avi*_*ash 6 c++ templates copy-constructor

如何为模板类编写复制构造函数.因此,如果模板参数是另一个用户定义的类,那么它的复制构造函数也会被调用.

以下是我的课

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)

K-b*_*llo 3

假设_TyV是一个值类型:

Vertex( Vertex const& src )
    : m_Label( src.m_Label )
{}
Run Code Online (Sandbox Code Playgroud)

顺便问一下,类实例中的这些名称不是由实现保留的吗?

C++ 标准保留一组名称供 C++ 实现和标准库使用 [C++ 标准 17.6.3.3 - 保留名称]。这些包括但不限于:

  • 包含双下划线的名称。
  • 以下划线开头,后跟大写字母的名称。
  • 在全局命名空间中以下划线开头的名称。