在不同文件中的C ++中的循环引用 [英] Circular references in C++ in different files

查看:92
本文介绍了在不同文件中的C ++中的循环引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想要一个循环引用,但在C ++中的两个不同的文件,我将如何实现?

If i want a circular reference but in two different files in C++, how would I implement that?

例如

AUnit.h

#inclue <BUnit.h>
class AClass : public TObject
{

   __published
        BClass * B;
};

BUnit.h

#include <AUnit.h>
class BClass : public TObject
{
    __published
        AClass *A;     
};

我不能只在一个包含转发声明的文件中。

I can't make it in only one file with forward declarations.

推荐答案

我假设你正在谈论循环依赖

答案确实是使用向前声明,例如:

The answer is indeed to use a forward declaration, such as:

AUnit.h

#include <BUnit.h>
class AClass : public TObject
{
   BClass *B;
};

BUnit.h

class AClass;  // Forward declaration

class BClass : public TObject
{
   AClass *A;
};

您甚至可以在两个头文件中

这篇关于在不同文件中的C ++中的循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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