C + + nested classes using (iii)

Source: Internet
Author: User

Reproduced in: http://www.2cto.com/kf/201110/109443.html

If a relationship between a nested type and its outer type requires member accessibility semantics, a C + + nested class is required, and a nested type should not perform tasks on a type other than its declared type, while the C + + local class allows classes, structs, and interfaces to be split into multiple small chunks and stored in different source files. Such implementations are easy to develop and maintain.

C + + partial class:

A class defined in a function body is called a local class. A local class can only be contacted with objects and functions in its perimeter scope because the variables in the perimeter scope are not related to the objects of that partial category (#add, what is the word?). Local classes cannot be inherited externally. When defining a local class, it is important to note that static member functions cannot be described in a local class, and all member functions must be defined in the class body (#add, since there is no other place to write the definition body). In practice, local classes are seldom used. The following is an example of a partial class.

int A;

void Fun ()

{

static int S;

Class A

{

Public

void init (int i) {s = i;}

};

A m;

M.init (10);

}

Another use of a local class is to implement a type conversion, as in the following code:

Class Interface

{

Public

virtual void fun () = 0;

};

Template <class T, class p>

interface* makeadapter (const t& obj, const p& ARG)

{

int x;

Class Local:public Interface

{

Public

Local (const t& obj, const p& ARG)

: Obj_ (obj), Arg_ (ARG) {}

virtual void Fun ()

{

x = 100;

Obj_. Call (Arg_);

}

Private

T obj_;

P Arg_;

};

return new Local (obj, arg);

}

C + + Nested classes:

A class defined in a class is called a C + + nested class, and a class that defines a nested class is called a perimeter class. The purpose of defining a nested class is to hide the class name and reduce the global identifier, thereby limiting the user's ability to use the class to establish an object. This improves the abstraction of the class and emphasizes the master-slave relationship between the two classes (the perimeter class and the nested Class). The following is an example of a nested class:

Class A

{

Public

Class B

{

Public

...

Private

...

};

void f ();

Private

int A;

};

Several descriptions of nested classes:

1, from the scope point of view, the nested class is hidden in the perimeter class, the class name can only be used in the perimeter class. If the class name is used within the scope of the perimeter class (#add that is outside the perimeter class), a name qualifier is required.

2. From the perspective of access rights, the nested class name has the same access rules as the object member name of its perimeter class. You cannot access a private member function in an object of a nested class, nor can you establish an object for a nested class in the private part of a perimeter class.

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

4. The members described in the nested class are not members of the objects in the perimeter class, and vice versa. The member functions of a nested class do not have access to members of the perimeter class, and vice versa. In this case, the nested class is often treated as a non-nested class when parsing the member access relationships of the nested class to the perimeter class. In this way, the nested classes described above can be written in the following format:

Class A

{

Public

void f ();

Private

int A;

};

Class B

{

Public

...

Private

...

};

#add Nested classes can access the static data of the perimeter class, even if the private

The nested class is only syntactically embedded, as is visible by the citation.

5. Friends that are described in C + + nested classes do not have access to members of the perimeter class.

6, if the nested class is more complex, you can only describe the nested classes in the perimeter class, the detailed contents of the nesting can be defined in a file field outside the body of the perimeter class.

C + + nested classes using (iii)

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.