C + + nested class use (i)

Source: Internet
Author: User

Reproduced in: http://www.cnblogs.com/charley_yang/archive/2011/04/05/2005897.html

First, nested class

   defines another class within one class, which we call a nested class (nested Class), or a nested type. The introduction of such a nested class is often due to the fact that the perimeter class needs to use a nested class object as the underlying implementation, and that the nested class is used only for the implementation of the perimeter class, and that the underlying implementation can be hidden from the user at the same time.
 
 
   Although nested classes are defined inside the perimeter class, it is a separate class that is basically not related to the perimeter class. Its members are not part of the perimeter class, and similarly, members of the perimeter class do not belong to that nested class. The appearance of nested classes only tells the perimeter class that there is one such type member for use by the perimeter class. Also,
 
 
 
   . For example, a static member of a nested class is one such example.
 
 
   previously said that another reason for using nested classes is to achieve the hidden purpose of the underlying implementation. To achieve this, we You need to define the nested class in another header file, but only in the perimeter class to declare the nested class . Of course, . When used, you only need to include this header file in the implementation file of the perimeter class.
 

    In addition, nested classes can refer directly to the static members, type names, and enumeration members of the perimeter class (assuming those members are public). The type name is a typedef name, an enumeration type name, or a class name.


Examples are as follows:

#ifndef Nestclass_h_
#define Nestclass_h_

Class A
{
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::~a ()
{

}

void A::operate ()
{
M_b = new B;
cout<< "A operate!" <<endl;
M_b->operate ();
}

#include "Nestclass.h"

void Main ()
{
A;
A.operate ();
}

We can only declare pointers and references to nested classes before the definition of the nested class is seen, as the above is defined in a as B m_b instead of b* M_b will throw a compilation error.

For detailed usage of C + + nested classes, refer to "C + + Primer third Edition" P551.

Second, the partial class

A class can also be defined in a function body such that a class is called a local class, and a local class is visible only within the local domain in which it is defined, unlike a nested class where there is no syntax to reference a member of a local class in a local field that defines the class, so the member function of the local class must be defined in the class definition, , this restricts the complexity of the member functions of the local class to a few lines of code, otherwise the code becomes difficult for the reader to understand.


Because there is no syntax to define members of a local class within a namespace domain, local classes are not allowed to declare static data members.

A class that is nested within a local class can be defined outside of its class definition, but the definition must appear in a local domain that contains a perimeter local class definition. The name of a nested class in a local domain definition must be decorated with its outer class name, and in the perimeter 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 a nested class is required
};

Nested class Definitions
Class Bar::nested {
// ...
};
}

The perimeter function does not have privileged access to private members of the local class, which, of course, can be achieved by making the perimeter function a friend of the local class.

As with nested classes, the names in the perimeter domains that the local class can access are also limited, and the local classes can access only the type names, static variables, and enumeration values defined in the perimeter local domain, for example:

int A, Val;

void foo (int val)
{
static int si;
Enum Loc {a = 1024x768, b};

Class Bar {
Public
Loc Locval; Ok
int barval;
void FooBar (Loc l = a) {//Ok:loc::a
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 resolution process that does not include the member function definition is to find the declaration that appears before the local class definition in the perimeter domain, and the parsing of the name in the member function body of the local class is: Before looking for the perimeter domain, first find the full domain of the class.


Still, if the first found declaration invalidates the use of the name, no other claims are considered, even if the use of Val in Foobar () is wrong, the compiler will not find the global variable Val unless it is qualified with the global domain resolution operator, such as: Val.

C + + nested class use (i)

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.