Two classes in C ++ reference each other solutions

Reprinted from --- "https://blog.csdn.net/xiqingnian/article/details/41214539

I. Description of the problem

There are two classes A and B need to be defined, when the need to use the definition of A B, B when the need to use the definition of A.

Second, analysis

A and B both define and call in a document is certainly not possible, this will cause an infinite loop cycle two calls.

The fundamental reason is: A definition of the time, there are A B, so we need to see the B accounts for space, but also found time to see the need to know the A accounts for space, resulting in an endless loop.

Solution:

(1) Write two headers Ah and Bh are used to declare classes A and B;

(2) Write .cpp files are used to define the two classes A and B;

(3) into the header in the header file B in A;

(4) A file header is not introduced in the header file B, but using extern manner declaring class A, and, B is used when A is in the form of pointers to use.

Principle: In pointer with B calls A, B then A needs to know the size of the space and they will go to find the definition file B, although the definition file B and A did not import the header file, do not know the A accounts space, but due to time in B calls a pointer form with a, B only know the pointer 4 bytes can be, do not need to know the size of the space a really, that is, a B also know the size of the space of.

I understand - "One must have a pointer through a pointer (4 bytes) + extern declaration of way, to ensure that, when compiled Bh, and through; so at compile time Ah, directly by the

Three, C ++ examples

A header file of Ah:

  1. #ifndef _A
  2. #define _A
  3. <strong>     # the include "of Bh" // A header file into a header file B </ strong>
  4. //extern class B;
  5. class A
  6. {
  7. private:
  8. int a;
  9. B objectB; header file // A head import file B, B when the call can not pointer
  10. public:
  11. A();
  12. you geta () ;
  13. void handle();
  14. };
  15. #endif _A


B header files Bh:

  1. #ifndef _B
  2. #define _B
  3. <strong> // # the include "Ah" B // A header file does not import header files, you need to have three things to note!
  4. extern class A ; // NOTE 1: extern statement needed A </ strong>
  5. class B
  6. {
  7. private :
  8. int b;
  9. * ObjectA A; // Note 2: A call when the need to use a pointer
  10. public :
  11. B();
  12. int getb () ;
  13. void handle();
  14. };
  15. # Endif _B


A definition file of A.cpp:

  1. #include <iostream>
  2. <strong> #include "A.h"</strong>
  3. using namespace std;
  4. A::A()
  5. {
  6. this->a= 100;
  7. }
  8. int A::geta()
  9. {
  10. return a;
  11. }
  12. void A::handle()
  13. {
  14. cout<< "in A , objectb.b="<<objectb.getb()<< endl;
  15. }


B's definition file B.cpp:

  1. #include <iostream>
  2. <strong> #include "B.h"
  3. # The include "Ah" // NOTE 3: A is introduced inside B.cpp headers </ strong>
  4. using namespace std;
  5. B::B()
  6. {
  7. this->b= 200;
  8. }
  9. int B::getb()
  10. {
  11. return b;
  12. }
  13. void B::handle()
  14. {
  15. objects = new A ();
  16. cout<< "in B , objecta->a="<<objecta->geta()<< endl;
  17. }


main.cpp:

  1. #include <iostream>
  2. #include <cstdlib>
  3. <strong> #include "A.h"
  4. // # include "Bh" // because Ah which already contains Bh, so this does not need to import the Bh. </ Strong>
  5. using namespace std;
  6. void main ()
  7. {
  8. A a;
  9. a.handle();
  10. B b;
  11. b.handle();
  12. system( "pause");
  13. }

Guess you like

Origin blog.csdn.net/qq_34326603/article/details/80995143