C ++ nested class

Source: Internet
Author: User

Access to nested classes:

Remember that the C ++ nested class is just syntax nested. However, this is not the case in practice.
Ex:

Class
{
Public:
Static int;
Class A1
{
Void output ()
{
Cout <A <Endl; // instead of a:;
}
};

};
Int A:;

It can be seen that there is no problem when Class A1 is embedded into Class A and the static variables accessed by Class A are not written to the peripheral domain. From the compilation perspective, this is within the scope of Class, you can find a in the symbol table (note that a must be static ). This is obviously different from the two classes written separately.

School of Computer Science, Tianjin University, Chang Xinglong MSN: cxl82116@msn.com

See the following code for a special example:

 

Ex:

Class
{
PRIVATE:
Int;
Class A1
{
Void output (a aobject)
{
Cout <aobject. A <Endl; // instead of a:;
}
};

};

This code can not be compiled in VC, but in DEV-C ++, that is to say, different compilations have different definitions on whether the nested class can access the Private Members of the peripheral class.

The same name of different scopes of Nested classes:

Class
{
Public:
Static int;
Class A1
{
Static int;
Int void output ()
{
Cout <A <Endl; // instead of a:;
}
};

};
Int A: A = 3;
Int A: A1: A = 4;

There is no problem with the output of internal A. If you want to access external, you can use a: A to specify the scope, and in the nested class, you can define static members.
You can access it using a: A1:.
Let's take a look at the Java situation.
Ex:

// This is a Java class
Class
{
Private int c = 2;
Class A1
{
Int c = 3;
Void output ()
{
System. Out. println (this. C );
System. Out. println (A. This. C );
}
}
}

From the definition, we can see that the definition of Java is dynamic and is based on the this pointer. Therefore, nested classes are not only syntactic, but also semantically subordinate, peripheral class members, including private members, are also visible to internal classes. Therefore, internal non-static classes cannot have static members, and such internal classes can be created only after the outer objects are created. Therefore, you can create objects like this:
Ex:

A A = new ();
A. A1 AA = A. New A1 ();

Or:
Ex:

A. A1 AA = new A (). New A1 (); // use an anonymous object

For static Nested classes
Ex:

 


// This is a Java class
Class
{
Private int c = 2; // (1)
Static Class A1
{
Static int c = 3;
Void output ()
{
// System. Out. println (this. c); // work well <--> A. a1.c; this result is easily explained by the theory of static compilation and dynamic loading relative address.
// System. Out. println (A. This. c); // (can't work), it is obvious that the peripheral object exists. You can change (1) to static through
System. Out. println (c); // work well
}
}
}

Compared with the definition of Java above, it can be seen that the token recognition behavior in C ++ is similar to the static class in Java, so we can guess that the class in C ++ is used for static storage. Therefore, the following statements can be easily obtained:

Ex:
// C ++
A: A1 * A = new A: A1 ();

 

Therefore, in C ++, internal classes can also have static objects.

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.