如何编写Template类副本构造函数 [英] How to write Template class copy constructor
本文介绍了如何编写Template类副本构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为模板类编写副本构造函数。因此,如果template参数是另一个用户定义的类,则它的副本构造函数也会被调用。
How to write copy constructor for a template class. So that if the template parameter is another user defined class it's copy constructor is also get called.
以下是我的类
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:
};
推荐答案
假设 _TyV
是值类型:
Vertex( Vertex const& src )
: m_Label( src.m_Label )
{}
实现中保留的类实例中的名称不是
Aren't those names within class instances reserved by the implementation, by the way?
C ++标准保留了一组名称供C ++实现和标准库使用[C ++标准17.6.3.3-保留名称]。其中包括但不限于:
The C++ standard reserves a set of names for use by C++ implementation and standard libraries [C++ standard 17.6.3.3 - Reserved names]. Those include but are not limited to:
- 包含双下划线的名称。
- 开头的名称
- 在全局名称空间中以下划线开头的名称。
这篇关于如何编写Template类副本构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文