A nested class of C + + means a class defined in a class that is similar to a member of a perimeter class and is decorated with the appropriate access modifier.
1 a nested class can directly orient a static member variable in a perimeter class.
2 Nested classes can access members of the perimeter class by reference, pointer, or object, regardless of whether the member is public,private,protected.
3 Nested classes cannot be defined in user code when they are modified by private because they are private.
4 the perimeter class does not have access to the non-public members of the nested class and has access to public members
5 The Perimeter class cannot access the public static members of a nested class directly, and must be preceded by a qualifier.
The sample program is as follows:
QT.h
#ifndefDDXX_QT_H #defineddxx_qt_hclass ba{class Sb;public:ba () ~ba () intbc;staticint BD;VOIDFUNP (sb* pSb);p rivate: int Ba;staticintbb;public:class sb{public:sb (); ~SB (); int sa;static int sb;voidfunc (ba* pBa);p Rivate:int SC; STATICINTSD;};}; #endif
QT.cpp
#include "QT.h" #include <iostream>using namespace std;intba::bd = 12;intba::bb = 13;INTBA::SB::SB = 21;INTBA::SB:: SD = 23;ba::ba () {BA = 1;BC = 2;} Ba::~ba () {}VOIDBA::FUNP (ba::sb* PSB) {PSB = new BA::SB ();p sb->sa;psb->sb;//psb->sc;//private//psb->sd;/ /privatedelete PSB;PSB = NULL;} BA::SB::SB () {}BA::SB::~SB () {}voidba::sb::func (ba* pBa) {cout<<bb<<endl;cout<<bd<<endl; PBa = new BA ();cout<<pba->ba<<endl;cout<<pba->bb<<endl;cout<<pba->bc< <ENDL;COUT<<PBA->BD<<ENDL;DELETEPBA;PBA = NULL;}
Main.cpp
#include <iostream> #include "QT.h" using namespace Std;void Main () {BA T; T.FUNP (NULL);//ba::sb when the modifier is private, it cannot be accessed externally ba::sbtb; Tb.func (NULL);}
Use of C + + nested classes