我在MSVC中查找了CList定义,afxtempl.h
并在MSDN上查找了文档。我没有看到CList& operator=(const CList&);
定义。
我可以直接operator=
用来复制CList对象吗?
CList<int> a = b;
还是我应该手动地从源CListhead
到目标CListtail
以及AddTail
在目标CList上进行迭代?
for(POSITION pos = a.HeadPosition(); pos; )
{
const auto& item = a.GetNext(pos);
b.AddTail(item);
}
任何建议都会有所帮助。谢谢。
如果未定义副本分配运算符,则不会定义它并且不能使用它。CList
正如您已经观察到的那样,这是正确的,因此,不能,您不能仅用于operator=
复制CList
对象。如果您想要集合的深层副本,则需要编写一个函数来手动进行。
但是,请考虑您是否真的想要深复制。大多数时候,您会希望通过引用而不是通过值来传递集合类型。在MFC中尤其如此,在MFC中,它们可以包含衍生自的对象,这些对象CObject
不一定要被复制。实际上,您会注意到CObject
,使用私有副本构造函数和赋值运算符,该类明确禁止复制:
// Disable the copy constructor and assignment by default so you will get
// compiler errors instead of unexpected behaviour if you pass objects
// by value or assign objects.
private:
CObject(const CObject& objectSrc); // no implementation
void operator=(const CObject& objectSrc); // no implementation
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句