C++循环引用问题

发布于 2024-09-29 08:16:43 字数 1080 浏览 8 评论 0原文

我有 2 个类:DataObjectDataElementDataObject(仅)保存指向 DataElement 的指针,并且 DataElement 包含指向多种类型的指针,其中 DataObject >。

这曾经没有问题,因为我只在 DataElement 中使用指向 DataObject 的指针,因此在 DataObject 的标头中向前声明 DataObject code>DataElement 就足够了。

但是现在,我尝试向 DataElement 添加一个析构函数,其中我需要对 DataObject 进行删除。对此,编译器说:

dataelement/destructor.cc: In destructor ‘DataElement::~DataElement()’:
dataelement/destructor.cc:8: warning: possible problem detected in invocation of delete operator:
dataelement/destructor.cc:8: warning: invalid use of incomplete type ‘struct DataObject’
dataelement/dataelement.h:7: warning: forward declaration of ‘struct DataObject’
dataelement/destructor.cc:8: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.

我该如何解决这个问题?前向声明显然是不够的,而我无法包含 DataObject 的完整标头,因为这再次给我带来了循环依赖。

提前致谢!

I have 2 classes: DataObject and DataElement. DataObject holds pointers to (only) DataElements, and a DataElement contains pointers to several types, among which a DataObject.

This used to be no problem, since I only use pointers to DataObjects in DataElement, so a forward declaration of DataObject in the header of DataElement is enough.

Now, however, I try to add a destructor to DataElement, in which I need a delete on a DataObject. On this the compiler says:

dataelement/destructor.cc: In destructor ‘DataElement::~DataElement()’:
dataelement/destructor.cc:8: warning: possible problem detected in invocation of delete operator:
dataelement/destructor.cc:8: warning: invalid use of incomplete type ‘struct DataObject’
dataelement/dataelement.h:7: warning: forward declaration of ‘struct DataObject’
dataelement/destructor.cc:8: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.

How can I solve this? A forward declaration is apparently not enough, while I cannot include the complete header for DataObject, since that gives me a circular dependency again.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再可℃爱ぅ一点好了 2024-10-06 08:16:43

在包含两个标头的 .cpp 文件中定义析构函数。

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

来日方长 2024-10-06 08:16:43

为在类体外部和第二个类之后定义的第一个类创建析构函数,例如

class DataElement;

class DataObject
{
    DataElement* elem;
public:
    ~DataObject();
};

class DataElement
{
    DataObject* obj;
public:
    ~DataElement() { delete obj; }
};

DataObject::~DataObject()
{
    delete elem;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文