重写CObList MFC的复制构造函数

Overriding Copy Constructor for CObList MFC

本文关键字:复制 构造函数 MFC CObList 重写      更新时间:2023-10-16

我在MFC工作,我有我自己的模板类(CDFAObList),它是从CObList派生的,可以接受我自己的类(CDFAObject)的成员,它是从CObject派生的。我需要覆盖编译器为cdfaobist生成的复制构造函数,因为它最终会工作到CObject,它具有私有复制和赋值函数,并给我这个:

1>error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
1>see declaration of 'CObject::CObject'
1>see declaration of 'CObject'
1>This diagnostic occurred in the compiler generated function 'CObList::CObList(const CObList &)'

即使我在CDFAObject中重载了复制构造函数和赋值操作符,它也会给我上面的错误。但是,当我试图覆盖CDFAObList的复制构造函数时,我得到以下编译器错误:

1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>see reference to class template instantiation 'CDFAObList<T>' being compiled

这是我的模板类:

#include "DFAObject.h"
#include "DFAManDoc.h"
#include "DFAManTreeView.h"
template<class T> class CDFAObList : public CObList
{
 public:
    CDFAObList(void) { }

    CDFAObList(CDocument* pDoc,CTreeCtrl* pTree, xml_document* pXmlDoc)
    {
        doc = pDoc;
        Tree = pTree;
        xmlDoc = pXmlDoc;
    }
    // problem copy constructor
    CDFAObList(const CDFAOblist<T>& toCopy)
    {
        doc = toCopy.doc;
        Tree = toCopy.tree;
        xmlDoc = toCopy.xmlDoc;
        for (int i = 0; i < toCopy->GetSize(); i++)
        {
            this->AddHead( (T*) toCopy->GetTail());
        }
    }
protected:
    CDocument* doc;
    CTreeCtrl* Tree;
    xml_document* xmlDoc;
};

我以前从未使用过类模板,所以我可能做错了一堆事情。谢谢你的帮助。

应该是

CDFAObList(const CDFAObList<T>& toCopy)

代替

CDFAObList(const CDFAOblist<T>& toCopy)