c ++包括避免循环引用 [英] c++ include avoid cyclic references

查看:152
本文介绍了c ++包括避免循环引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个班级:A,B和C. C是 #include 由B编辑,B是 #includ 由A编辑。在C类中我定义了一个按钮的处理程序,当按下按钮时,C将 PostMessage 添加到对象A.如果我包含A在C中,我将有一个循环引用,那么我应该怎么做才能避免这种循环引用?

I have 3 classes: A, B and C. C is #includeed by B, and B is #included by A. In class C i have defined a handler for a button, and when the button is pushed, C will PostMessage to object A. If i include A in C, i will have a cyclic reference, so what should i do to avoid this cyclic reference?

编辑:所有包含都是在实现文件中进行的。

All includes are made in implementation files.

推荐答案

您应该使用前向声明。由于 C 不是 A 的所有者,我假设你有一个指针作为成员。所以你不需要包括:

You should use forward declarations. Since C is not the owner of A, I'll assume you have a pointer as a member. So you don't need to include:

class A; //forward declaration
class C
{
    A* a;
};

在实施文件中,您将包含 Ah 但没关系。另外,如果可以,请尽可能在 A.h B.h 中使用前向声明。

In the implementation file, you'll include A.h but that's OK. Also, if you can, use forward declarations in A.h and B.h where possible.

这篇关于c ++包括避免循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆