Two classes and inline functions

前端 未结 3 933
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-03 05:52

I have two classes and both of them uses some of the other class, on example:

// class1.h
class Class1;
#include \"class2.h\"

class Class1 {
  public:
  sta         


        
相关标签:
3条回答
  • 2021-01-03 06:16

    You have it mix'd up. What you want is:

    // class1.h
    class Class2;
    
    class Class1 {
      public:
      static Class2 *C2;
      ...
    };
    
    // class2.h
    class Class1;
    
    class Class2 {
      public:
      static Class1 *C1;
      ...
    };
    

    And include the respective headers in the source. The line:

    class Class1; // or Class2
    

    Declares an incomplete type, and you can have pointers and references to incomplete types. Upon usage, though, it needs to be complete. So just say "hey it'll exist!" in the header, and in the source tell it what it is.

    0 讨论(0)
  • 2021-01-03 06:26

    My suggestion is that you place common methods and members into a base class, then derive C1 and C2 from the base class. This may fix the circular dependency issue.

    0 讨论(0)
  • 2021-01-03 06:27

    You need to delay including the header, but then include it and define your inline methods. By doing this in each header, they are self-sufficient and including one will always include the other, with include guards preventing infinite recursion.

    A.hpp

    #ifndef INCLUDE_GUARD_B9392DB18D114C1B8DFFF9B6052DBDBD
    #define INCLUDE_GUARD_B9392DB18D114C1B8DFFF9B6052DBDBD
    
    struct B;
    
    struct A {
      B* p;
      void foo();
    };
    
    #include "B.hpp"
    
    inline
    void A::foo() {
      if (p) p->bar();
    }
    
    #endif
    

    B.hpp

    #ifndef INCLUDE_GUARD_C81A5FEA876A4C6B953D1EB7A88A27C8
    #define INCLUDE_GUARD_C81A5FEA876A4C6B953D1EB7A88A27C8
    
    struct A;
    
    struct B {
      A* p;
      void bar();
    };
    
    #include "A.hpp"
    
    inline
    void B::bar() {
      if (p) p->foo();
    }
    
    #endif
    
    0 讨论(0)
提交回复
热议问题