Local class and nested class

Source: Internet
Author: User


Local class

Classes defined in a function are called local classes. In a local class, only objects and functions in its peripheral scope can be associated, because the variables in the peripheral scope are irrelevant to the objects in the department class. When defining a local class, note that static member functions cannot be described in the local class, and all member functions must be defined in the class body. In practice, local classes are rarely used. The following is an example of a local class.

Int;
Void fun ()
{
Static int S;
Class
{
Public:
Void Init (int I) {S = I ;}
};
A m;
M. INIT (10 );
}

Nested class

A class defined in a class is called a nested class, and a class defining a nested class is called a peripheral class.

The purpose of defining a nested class is to hide the class name and reduce the global identifier, so as to limit whether users can use this class to create objects. This improves the class abstraction capability and emphasizes the master-slave relationship between two classes (peripheral class and nested class. The following is an example of a nested class:

Class
{
Public:
Class B
{
Public:
...
PRIVATE:
...
};
Void F ();
PRIVATE:
Int;
}

Class B is a nested Class, Class A is a peripheral class, and Class B is defined in Class.

Several descriptions of Nested classes:

1. From the perspective of scope, the nested class is hidden in the peripheral class, and the class name can only be used in the peripheral class. If you use this class name within the scope of the peripheral class, you need to apply a name restriction.

2. From the perspective of access permission, the nested class name has the same access permission rules as the object member name of its peripheral class. You cannot access private member functions of nested class objects or create objects for nested classes in the private section of the peripheral class.

3. The member functions in the nested class can be defined in the external body of the class.

4. The members in the nested class are not members of objects in the peripheral class, and vice versa. The member functions of the nested class have no access to the members of the peripheral class, and vice versa. When analyzing the access relationships between Nested classes and peripheral classes, the nested classes are often treated as non-nested classes. In this way, the preceding Nested classes can be written in the following format:

Class
{
Public:
Void F ();
PRIVATE:
Int;
};

Class B
{
Public:
...
PRIVATE:
...
};

As you can see, nested classes are only syntactically embedded.

5. The friends indicated in the nested class do not have access to the members of the peripheral class.

6. If the nested class is complex, you can only describe the nested class in the peripheral class. The detailed content about nesting can be defined in the file domain of the peripheral class in vitro.

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.