C + + nested class and local class parse _c language in detail

Source: Internet
Author: User

1. Nested class
The perimeter class needs to use a nested class object as the underlying implementation, and the nested class is used only for the implementation of the perimeter class, and the underlying implementation can be hidden from the user at the same time.
From a scope perspective, nested classes are hidden in the perimeter class, which can only be used in the perimeter class. If you use the class name in a scope other than the perimeter class, you need to add a name qualifier.

A member function in a nested class can be defined outside its class body.

A member function of a nested class has no access to the private members of the perimeter class, and vice versa.

Nested classes are only syntactically embedded.

2. Local class
A class can also be defined in a function body, and such a class is called a local class (Loacl Class). A local class is visible only within the local domain in which it is defined.

The member functions of the local class must be defined in the class body.

A static member function cannot be in a local class.

In practice, local classes are rarely used.

Here's a piece of code to illustrate:

Copy Code code as follows:

#include <iostream>
using namespace Std;

Class Outer
{
Public
Class Inner
{
Public
void Fun ();
};
Public
Inner obj_;
void Fun ()
{
cout<< "Outer::fun ..." <<endl;
Obj_. Fun ();
}
};

void Outer::inner::fun ()
{
cout<< "Inner::fun ..." <<endl;
}

void Fun ()
{
Class Localclass
{
Public
int num_;
void Init (int num)
{
Num_=num;
}
void Display ()
{
cout<< "Num_" <<num_<<endl;
}
};
Localclass LC;
Lc. Init (10);
Lc. Display ();
}

int main ()
{
Outer o;
O.fun ();
Outer::inner i;
I.fun ();
Fun ();
return 0;
}


Run Result:

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.