C + + nested class use (ii)

Source: Internet
Author: User

Reproduced in: http://www.cnblogs.com/chengmin/archive/2010/03/22/1691955.html

Access issues for nested classes:

Remember that there is one sentence in C + + that is in Baifeng boiling: C + + Nested classes are only syntactic nesting. In practice, however, this is not the case.
Ex:

class a{public:      staticint  A;     class A1    {        void  output ()        {          cout//instead of a::a;         }    };  }; int a::a;

It can be seen that the class A1 embedded a after the static variable access a does not have any problem with the perimeter domain, from a compilation point of view, at this point in the scope of a:: In the symbol table is to find a (note that a must be static). This is significantly different from the two classes that are written separately

There is a special point, see the following code:

Ex:

class a{private:    int  A;     class A1    {        void  output (A aobject)        {          cout//instead of a::a;         }    };  };

This code can not be compiled in the VC, but in dev-c++ is possible, that is, the different compilation for the nested class can access the private members of the perimeter class is not consistent with the definition.

Different scopes for nested classes have the same name problem:

classa{ Public:      Static intA; classA1 {Static intA; int    voidoutput () {cout<<a<<endl;//instead of a::a;        }    }; };intA::a=3;intA::a1::a=4;

There is no problem with the output internal, if you want to access the external, you can use A::A to indicate scope, and in nested classes, you can define static members.
It can be accessed with similar a::a1::a.
Let's take a look at the Java situation.
Ex:

 // this is a Java class  class   a{ private  int  c=2  ;  class   A1 { int  c=3  ;  void   output () {System.  out . println (this             .c); System.  out . println (A.this  .C); }    }}

As can be seen by definition, Java is defined dynamically and is based on the this pointer, so nested classes are not only syntactically, but also semantically subordinate, members of the perimeter class, including private members, are also visible to inner classes. Therefore, an internal non-static class cannot have a static member, and such an inner class cannot be established until the outer object is established, so you can create an object like this:
Ex:

New= A.new A1 ();

Or:
Ex:

New A (). New // using anonymous objects

If it is a static nested class
Ex:

//This is a Java classclassa{Private intC=2;//(1)    Static classA1 {Static intC=3; voidoutput () {//System.out.println (THIS.C);//Work well <--> a.a1.c; This result is easily explained by compiling static genetic and dynamically loading the relative address theory//System.out.println (A.THIS.C);//(can ' t work), it is clear that the peripheral object exists, the (1) is changed to static can be passedSystem. out. println (c);// work well        }    }}

In contrast to the above Java definition, it can be seen that the 黓 behavior in C + + is similar to the static class in Java, thus, it is possible to guess that the classes in C + + are statically stored. Therefore, it is possible to easily draw the following statement:

Ex: // C + + New A::A1 ();

Therefore, it can also be obtained in C + +, inner classes can also have static objects.

C + + nested class use (ii)

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.