Defines a Cbottle class in which a member of another class Ccarton operates on the Cbottle, thus giving the friend rights of the Ccarton member in the Cbottle class. We can easily write the following code:
The header file of the Cbottle class bottle.h
#include "carton.h"
class Ccarton;
Class cbottle{public
:
cbottle (double height, double diameter): m_height (height), m_diameter (diameter) {
private:
double m_height;
Double M_diameter;
Friend Ccarton::ccarton (const cbottle& abottle);
<pre name= "code" class= "CPP" >//ccarton header file for the class Carton.h
#include "bottle.h"
class Cbottle;
Class ccarton{public
:
Ccarton (const cbottle& abottle) {
m_height = abottle.m_height;
M_length = 4.0 * abottle.m_diameter;
M_width = 3.0 * abottle.m_diameter;
}
Private:
double m_height;
Double m_length;
Double m_width;
};
The file that contains the main function main.cpp
#include "carton.h" #include "bottle.h" int main () {Cbottle abottle (10, 20); Ccarton Acarton (abottle); return 0;}
The run result shows an error because the first statement encountered when executing bottle.h is #include "carton.h" and jumps to the "carton.h" file (at which point the "bottle.h" file has not yet been executed, that is, it has not been successful), and "Carton.h "The first statement of the file is #include" bottle.h ", the compiler jumps to the" bottle.h ", so that depth=1024, the compiler falls into a dead loop. This occurs because of the inclusion of two header files, and if only one contains another, the compiler can pass smoothly. We change the "carton.h" file because the Ccarton constructor in "Carton.h" uses the specific object of the Cbottle class, so it must contain "bottle.h" if we simply declare the Ccarton constructor without specifically implementing the constructor, You do not need to #include "bottle.h", we place the implementation of the Ccarton constructor in Carton.cpp so that the compiler can preprocess two. h files.
The header file of the Cbottle class bottle.h #pragma once #include "carton.h" class Ccarton; Class cbottle{Public:cbottle (double height, double diameter): m_height (height), m_diameter (diameter) {} Private:doubl
e m_height;
Double M_diameter;
Friend Ccarton::ccarton (const cbottle& abottle); };<pre name= "code" class= "CPP" ><pre name= "code" class= "CPP" ><pre name= "code" class= "CPP" ><pre
Name= "code" class= "CPP" >//ccarton class header file Carton.h #pragma once//#include "bottle.h" class Cbottle;
Class ccarton{Public:ccarton (const cbottle& abottle); private:double m_height;
Double m_length;
Double m_width;
}; <pre name= "code" class= "CPP" ><pre name= "code" class= "CPP" >//ccarton the source file of the class Carton.cpp #include <iostream
> #include "carton.h" #include "bottle.h" Ccarton::ccarton (const cbottle& abottle) {m_height = Abottle.m_height;
M_length = 4.0 * Abottle.m_diameter;
M_width = 3.0 * Abottle.m_diameter;
Std::cout << "Carton constructor success\n\n"; } <pRe name= "code" class= "CPP" ><pre name= "code" class= "CPP" >//main function main.cpp #include <iostream> #include "
Carton.h "#include" bottle.h "using namespace std;
int main () {Cbottle abottle (10, 20);
Ccarton Acarton (Abottle);
System ("pause");
return 0; }
Note: Be sure to add #pragma once in this case, because the header file contains relationships and the class's predecessor declaration, it is easy to redefine the class. The #pragma once directive ensures that the definition of a class can only occur once during compilation.
Similarly, with regard to class dependencies, the following article is well said and can be hung here
(Application of pre-declaration in C + + and trap-click to open link http://blog.csdn.net/yunyun1886358/article/details/5672574)