First, the problem description
There are now two classes A and b that need to be defined, B is required when defining a, and a is needed when defining B.
Second, analysis
The definition and invocation of a and B are definitely not allowed in a file, which will result in a dead loop of two loop calls.
The root cause is: When you define a, A has a B in it, so we need to look at the space of B, but when we look at it, we need to know the space size of a, causing a dead loop.
Workaround 1:
(1) Write two header files A.h and B.h respectively for declaring classes A and B;
(2) write two. cpp files to define classes A and B, respectively;
(3) The header files of a and B are respectively imported into the header file.
Workaround 2:
(1) Write two header files A.h and B.h respectively for declaring classes A and B;
(2) write two. cpp files to define classes A and B, respectively;
(3) The header file of B is imported into the header file of A;
(4) In the header file of B does not import a header file, but the extern way to declare Class A, and in B in the use of a in the form of pointers.
principle: in B with the pointer to a, then in a need to know the space size B, to find the definition of B file, although B's definition file does not import a header file, do not know the space of a, but in B call a in the form of a pointer, b know that the pointer occupies 4 bytes, So a is also aware of the space size of B. Summary: When you need a class, but do not import a class header file does not know that the class occupies the size of the space, you can declare the class with extern, and invoke the method of this class with pointers.
three, C + + examples
A's header file A.h:
#ifndef _a#define _a#include "B.h"//a header file imported the header file of B//extern class B;class a{private:int A; B objectb;<span style= "font-family:arial, Helvetica, Sans-serif;" >//a header file Import B's header file, in the call B can be used without pointers </span>public:a (); int geta (); void handle ();}; #endif _a
B's header file B.h:
#ifndef _b#define _b//#include "A.h"//b header file does not import a header file, need to have three places to pay attention! extern class a;//Note 1: Need to use extern declaration AClass B{private:int B; A * objecta;//NOTE 2: Call A When you need to use the pointer public:b (); int getb (); void handle ();}; #endif _b
A's definition file A.cpp:
#include <iostream> #include "A.h" using namespace std; A::a () {this->a=100;} int A::geta () {return A;} void A::handle () {cout<< "in A, objectb.b=" <<objectb.getb () <<endl;}
B's definition file B.cpp:
#include <iostream> #include "B.h" #include "A.h" <span style= "font-family:arial, Helvetica, Sans-serif;" >//Note 3: Import a header file inside B.cpp </span>using namespace std; B::b () {this->b=200;} int B::getb () {return B;} void B::handle () {objecta=new A ();cout<< "in B, objecta->a=" <<objecta->geta () <<endl;}
Main.cpp:
#include <iostream> #include <cstdlib> #include "A.h"//#include "B.h" <span style= "font-family:arial, Helvetica, Sans-serif; " >//because the A.h contains B.h, so there is no need to import B.h here. </span>using namespace Std;void Main () {A a;a.handle (); B B;b.handle (); System ("Pause");}
Operation Result:
Four, the simplest method
The simplest way is to import the header file of B in a header file, import a header file in the B header file, this time has the following advantages: (1) do not need to use the pointer when the call, (2) do not need to use extern.
Workarounds for two classes referencing one another in C + +