Use of C + + nested classes

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.