C++ circular reference problem

后端 未结 2 1575
走了就别回头了
走了就别回头了 2021-01-27 16:47

I have 2 classes: DataObject and DataElement. DataObject holds pointers to (only) DataElements, and a DataElement

相关标签:
2条回答
  • 2021-01-27 17:21

    Make the destructor for the first class defined outside of the class body and after the second class, e.g.

    class DataElement;
    
    class DataObject
    {
        DataElement* elem;
    public:
        ~DataObject();
    };
    
    class DataElement
    {
        DataObject* obj;
    public:
        ~DataElement() { delete obj; }
    };
    
    DataObject::~DataObject()
    {
        delete elem;
    }
    
    0 讨论(0)
  • 2021-01-27 17:31

    Define the destructor in a .cpp file that includes both headers.

    0 讨论(0)
提交回复
热议问题