How can I declare classes that refer to each other?

前端 未结 2 1547
一个人的身影
一个人的身影 2020-12-12 01:26

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

Right now I have something like:

a.h

相关标签:
2条回答
  • 2020-12-12 02:06

    You have to use Forward Declaration:

    a.h

    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.

    0 讨论(0)
  • 2020-12-12 02:18

    Use forward declaration : http://en.wikipedia.org/wiki/Forward_declaration

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