Introduction:
In a special case, when two classes need to reference each other to form a "ring" reference, it is not possible to define the use first. We need to use a forward statement.
However, the class of the forward declaration cannot be instantiated.
1 #pragma once 2 3 #include " b.h 4 class A { 6 public : A (); ~a (); 10 B B_ ; 11 };
#pragma once"A.h"class b{public: B (); ~B (); A A_;};
Two classes that contain a compilation hint error are:
1 1>------Started Build: Project: Test, configuration: Debug Win32------2 1>B.cpp3 1>g:\cpp\ Code \TEST\TEST\A.H (Ten): Error C2146: syntax error: Missing ";" (in front of the identifier "B_")4 1>g:\cpp\ Code \TEST\TEST\A.H (Ten): Error C4430: Missing type specifier-assumed to beint。 Note: C + + does not support the defaultint5 1>A.cpp6 1>g:\cpp\ Code \test\test\b.h (9): Error C2146: syntax error: Missing ";" (in front of the identifier "A_")7 1>g:\cpp\ Code \test\test\b.h (9): Error C4430: Missing type specifier-assumed to beint。 Note: C + + does not support the defaultint8 1>Generating Code ...9========== Generation: Success0One, failure1A, Latest0One, skip0A ==========
So what is a forward statement?
#pragma once"A.h"class //forward declaration class b{ public: B (); ~B (); //Non-instancing };
This is still wrong, because the forward declaration is not instantiated.
Compile hints:
1>------Started Build: Project: Test, configuration: Debug Win32------1>B.cpp1>g:\cpp\ Code \TEST\TEST\A.H (Ten): Error C2146: syntax error: Missing ";" (in front of the identifier "B_")1>g:\cpp\ Code \TEST\TEST\A.H (Ten): Error C4430: Missing type specifier-assumed to beint。 Note: C + + does not support the defaultint1>A.cpp1>g:\cpp\ Code \test\test\b.h ( One): Error C2079: "B::a_" using undefinedclass"A"1>Generating Code ...========== Generation: Success0One, failure1A, Latest0One, skip0A ==========
Very simple processing, only need to use the pointer. It is also possible to pass a trial reference.
1 #pragmaOnce2#include"A.h"3 4 classA;5 classB6 {7 Public:8 B ();9~B ();Ten OneAA_; A};
Nested classes: Demo
1#include <iostream>2 using namespacestd;3 4 5 classOuter6 {7 classInner8 {9 Public:Ten voidFun () One { Acout <<"inner:fun ...."<<Endl; - } - }; the - Public: - Inner obj_; - voidFun () + { -cout <<"outer:fun ..."<<Endl; + Obj_.fun (); A } at - }; - - intMainvoid) - { - Outer o; in O.fun (); -}
# # From a scope perspective, nested classes are hidden in the perimeter class, and the class name can only be used in a perimeter class. If you use the class name in the scope of the perimeter class, you need to add a name qualifier;
# #嵌套类中的成员函数可以再它的类体外定义;
# #嵌套类的成员函数对外围类的的成员没有访问权, and vice versa.
# #嵌套类仅仅是语法上的嵌入
1#include <iostream>2 using namespacestd;3 4 5 classOuter6 {7 classInner8 {9 Public:Ten voidFun (); One }; A - Public: - Inner obj_; the voidFun () - { -cout <<"outer:fun ..."<<Endl; - Obj_.fun (); + } - + }; A voidOuter::inner::fun () at { -cout <<"inner:fun ...."<<Endl; - } - - intMainvoid) - { in Outer o; - O.fun (); to}
Out-of-class declarations only need to remember to add multiple class names.
C + + Learning path: Forward statement