我如何声明相互引用的类? [英] How can I declare classes that refer to each other?

查看:181
本文介绍了我如何声明相互引用的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我做C ++以来,我花了很长时间,我遇到了一些麻烦的类引用彼此。

It's been a long time since I've done C++ and I'm running into some trouble with classes referencing each other.

现在我有类似的:

ah

class a
{
  public:
    a();
    bool skeletonfunc(b temp);
};

bh

class b
{
  public:
    b();
    bool skeletonfunc(a temp);
};

由于每一个都需要引用另一个,我发现我不能做一个#包括彼此在顶部或我结束了一个怪异的循环与包括。

Since each one needs a reference to the other, I've found I can't do a #include of each other at the top or I end up in a weird loop of sorts with the includes.

所以我怎么做,使

So how can I make it so that a can use b and vice versa without making a cyclical #include problem?

推荐答案

您必须使用前进声明

ah

class b;
class a
{
  public:
    a();
    bool skeletonfunc(b temp);
}

然而,在许多情况下,这可能会迫使您使用引用或指针在您的方法调用或成员变量中,因为您不能在两个类标题中有完整的类型。如果必须知道类型的大小,则需要使用引用或指针。但是,如果只需要方法声明,则可以使用类型。

However, in many situations, this can force you to work with references or pointers in your method calls or member variables, since you can't have the full types in both class headers. If the size of the type must be known, you need to use a reference or pointer. You can, however, use the type if only a method declaration is required.

这篇关于我如何声明相互引用的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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