C + + Internal classes

Source: Internet
Author: User

An inner class is actually a kind of local data type defined within a class declaration.

Declarations of----inner classes are of public and private

If declared as public, it can also be used outside to define variables, such as Outer::inner var

If the declaration is private, then the outside can not be used to define the variable, then outer::inner var will cause a compilation error.

----the inner class is declared, it can be used to define the variable

This is the same as other data type definition variables, and access rules are the same. Without him

----Mutual access of internal classes and external classes

cannot be accessed and is completely dependent on the definition property of the member variable.

----For example

1 #include <iostream>
2 using namespace Std;
3
4 Class A
5 {
6 Public:
7 Class B1
8 {
9 Public:int A;
Ten Private:int B;
public:void foo (A &p) {
cout << p.i1 << Endl; OK, because I1 is public in class A
cout << p.i2 << Endl; Fail, because I2 is private in class A
14}
15};
16
+ Private:
Class B2
19 {
Public:int A;
Private:int b;
public:void foo (A &p) {
cout << p.i1 << Endl; OK, because I1 is public in class A
cout << p.i2 << Endl; Fail, because I2 is private in class A
25}
26};
27
Public:
B1 B11;
B2 B12;
I1 int;
Private:
B1 B21;
B2 B22;
I2 int;
Public:
PNS void F (b1& p) {
cout << p.a << Endl; OK, because a is public in class B1
cout << p.b << Endl; Fail, because B is private in class B1
40}
b2& void F (p) {
cout << p.a << Endl; OK, because a is public in class B2
cout << p.b << Endl; Fail, because B is private in class B2
44}
45};
46
int main (int argc, char *argv[])
48 {
A;
A::B1 AB1; OK, because B1 is declared as public inner class.
Wuyi A::B2 ab2; Fail, because B2 is declared as private inner class
return 0;
53}

http://blog.csdn.net/hihui/article/details/4822412

C + + Internal classes

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.