C ++ Summary of precautions related to "class" (6): Nested class and local class

Source: Internet
Author: User

1. Nested classes

 

Define another class within a class, which is called the nested class or the nested type. The reason for introducing such a nested class is that the peripheral class must use the nested class object as the underlying implementation, and the nested class is only used for the implementation of the peripheral class, this underlying implementation can also be hidden from users.
 
 
Although the nested class is defined inside the peripheral class, it is an independent class and is basically irrelevant to the peripheral class. Its members do not belong to the peripheral class. Similarly, the members of the peripheral class do not belong to the nested class. The appearance of Nested classes only tells the peripheral class that there is such a type member for use by the peripheral class. In addition, the access of the peripheral class to the nested class members is not privileged, and the access of the nested class to the peripheral class members is the same. They all follow the label access control of the common class.
 
 
 
If its members are not defined within the nested class, the definition can only be written to the same scope as the peripheral class, and must be limited by the peripheral class. The definition cannot be written in the peripheral class. For example, a static member of a nested class is an example.
 
 
As mentioned above, another reason for using nested classes is to achieve the underlying implementation of hiding. To achieve this purpose, we need to define this nested class in another header file, and only declare this nested class in the front of the peripheral class. Of course, when defining this nested class outside the peripheral class, you should use the peripheral class for limitation. In use, you only need to include this header file in the implementation file of the peripheral class.
 

In addition, nested classes can directly reference static members, type names, and enumeration members of peripheral classes (assuming these members are public ). The type name is a typedef name, enumeration type name, or a class name.


Example:
 

# Ifndef nestclass_h _
# Define nestclass_h _

Class
{
Public:
A ();
~ A ();
 
Void operate ();
PRIVATE:
Class B;
B * m_ B;
};
 
# Endif

 
# Include "nestclass. H"
# Include <iostream>
Using namespace STD;
 
Class A: B
{
Public:
B (){}
~ B (){}
 
Void operate ()
{
Cout <"B operate! "<Endl;
}
};
 
A: ()
{
 
}
 
A ::~ A ()
{
 
}
 
Void A: Operate ()
{
M_ B = new B;
Cout <"A operate! "<Endl;
M_ B-> operate ();
}

# Include "nestclass. H"

Void main ()
{
A;
A. Operate ();
}
 
Before the definition of the nested class is seen, we can only declare the pointer and reference of the nested class. For example, the above definition of B m_ B rather than B * m_ B in a will cause a compilation error.

For detailed usage of C ++ Nested classes, see C ++ Primer 3 p551.

 

Ii. Local class

 

A class can also be defined in a function. A class is called a local class. A local class is visible only in the local domain where it is defined. Unlike a nested class, no syntax can reference the members of a local class when defining this class. Therefore, member functions of a local class must be defined in the class definition. In practice, this limits the complexity of member functions of local classes to several rows.CodeOtherwise, the code will become hard to understand for readers.

Because there is no syntax to define members of a local class in the namespace domain, it is not allowed to declare static data members for a local class.

A nested class in a local class can be defined outside its class definition. However, this definition must appear in a local area that contains the peripheral local class definition. The name of the nested class in the local domain definition must be limited by its peripheral class name. In the peripheral class, the declaration of the nested class cannot be omitted. For example:

Void Foo (INT Val)
{
Class bar {
Public:
Int barval;
Class nested; // The Declaration of Nested classes is required
};
 
// Nested class definition
Class bar: Nested {
//...
};
}

 

The peripheral function has no privilege to access private members of a local class. Of course, this can be achieved by making the peripheral function a friend of a local class.

Like Nested classes, the names of peripheral domains that can be accessed by local classes are limited. Local classes can only access the type names, static variables, and enumeration values defined in the peripheral local domains. For example:

 

Int A, Val;
 
Void Foo (INT Val)
{
Static int Si;
Enum loc {A = 1024, B };
 
Class bar {
Public:
Loc locval; // OK;
Int barval;
Void foobar (loc L = A) {// OK: LOC:
Barval = val; // error: local object
Barval =: Val; // OK: Global Object
Barval = SI; // OK: static local object
Locval = B; // OK: enumeration Value
}
};
//...
}

 

In a local class, the name parsing process that does not include the member function definition is to find the declaration that appears before the local class definition in the peripheral domain, the parsing process of the name in the member function of a local class is that the complete domain of the class is directly searched before the peripheral domain is searched.

 
The same is true. If the declaration found first makes the usage of the name invalid, other statements are not considered, even if Val is used in foobar, the compiler does not find the global variable Val, unless it is qualified by the fully-local resolution operator, such as: Val.

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.