Class C ++ is nested with the external class and friends.

Source: Internet
Author: User

[Conversion] Class C ++ is nested with the external class and friends.
[Turn] http://baike.baidu.com/link? Url = Md223wQoT5s-3cZ5xRnj1pGmvm310DKAuh-HDrcEdc2l24rwobHrdEc_Mi4Z3BGP0jxRqTCBQkRXJoGtuWNS7 _[Reprint] http://www.cnblogs.com/qzhforthelife/archive/2013/07/31/3226885.html1.1 The definition of a nested class is called a nested class in a class body. A class with nested classes is called a peripheral class. 1.2 The purpose of the nested class is to define the nested class. The original intention is to hide the class name and reduce the global identifier, so as to limit whether users can use the class to create objects. This improves the class abstraction capability and emphasizes the master-slave relationship between two classes (peripheral class and nested class. 1.3 examples of Nested classes

1 # include <iostream> 2 using namespace std; 3 4 class A 5 {6 public: 7 class B 8 {9 public: 10 B (char * name) {11 cout <"constructing B:" <name <endl; 12} 13 void printB (); 14}; 15 B B; 16 (): B ("In class A") {// constructor 17 cout <"constructing A" <endl; 18} 19}; 20 21 void A: B:: printB () {22 cout <"B's member function" <endl; 23} 24 25 int main (int argc, char * argv []) 26 {27 A; 28 a: B B ("outside of A"); 29 B. printB (); 30}
Program output result:
1 constructing B:In class A2 constructing A3 constructing B:outside of A4 B's member function
Several descriptions of Nested classes: (1) from the scope perspective, nested classes and peripheral classes are completely independent of each other. They only have a master-slave relationship and cannot access each other, there is no friend relationship either. (2) From the Perspective of access permissions, nested classes can be both private and public. In the preceding example, the access permission of the nested Class B is public. You can use this nested class outside the member functions of the peripheral class, and add the name limitation when using it. If the access permission of nested Class B is set to private, it can only be used within the peripheral class. (3) member functions in a nested class can be defined in the external body of the class. (4) The nested class can access the static member variables of the peripheral class. Even if its access permission is private, the access method is directly accessed through "ClassName: staticVarName.
 1 #include<iostream> 2 using namespace std; 3  4 class A 5 { 6 public: 7     A(){ 8         cout << "A construct" <<endl; 9     }10     class B{11     public:12         B(){13             cout << "B construct" <<endl;14         }15     };16 };17 18 int main()19 {20     A ca;21     cout<<"-------------"<<endl;22     A::B b;23     return 0;24 }

Program output result:

1 A construct2 -------------3 B construct

We can see that when creating a peripheral Class Object, only the peripheral class constructor is executed, instead of the nested class constructor and then the peripheral class constructor. Nested classes are different from inherited or member objects.

 

[Reprint] http://www.cnblogs.com/qzhforthelife/archive/2013/07/31/3226885.html

First run the Code:

1 class Outer 2 {3 private: 4 int m_outerInt; 5 public: 6 Outer () {m_outerInt = 0;} 7 8 // internal class definition starts 9 class Inner10 {11 public: 12 Inner () {m_innerInt = 1;} 13 private: 14 int m_innerInt; 15 public: 16 void DisplayIn () {cout <m_innerInt <endl ;}17 }; 18 // End internal class 19 void DisplayOut () {cout <m_outerInt <endl ;}20}; 21 22 int main () 23 {24 Outer out; 25 Outer :: inner in; 26 out. displayOut (); 27 in. displayIn (); 28 29 return 0; 30}

As shown in the code above, in this case, the external class is not closely related to the internal class. The external class only limits the scope of the internal class name, you can use the internal class just like any other class after adding the Outer limitation,Outer is just a namespace for Inner.

Q: In the code above, how does an Inner member function (such as DisplayIn) access data members of an external class (Outer?

A: Before asking this question, you must first understand the fact that, in the future, you will call the Inner member function on an Inner instance object, the so-called "access to external class data members" is unreasonable. The "external class" and any class are just code. It is a description. From the memory perspective, after the program runs, the code is stored in the code area. Therefore, you should ask "How to access data members of external class instances" (data members are for each object, you must first have an external class instance (or instance pointer) before talking about access.

In the next step, if you do not care about sanqi 21, add the following line to the Inner's DisplayIn method:

1 m_outerInt=10;

Then you compile and link all through (in fact this is not possible), then in the main function:

1 int main()2 {3     Outer::Inner in;4     in.DisplayIn();5 6     return 0;7 }

In this case, you can run normally. Why? Which instance of data is m_outerInt in DisplayIn?

Therefore, in order to avoid such a ridiculous occurrence, the syntax layer has made the above impossible: the compilation will not pass.

Q: Can I solve the problem after setting Inner in the above Code as Outer's friend Meta class?


A: This questioner not only made the first questioner's mistake, but also misunderstood the meaning of youyuan.

Youyuan example:

1 class Inner; 2 3 class Outer 4 {5 private: 6 int m_outerInt; 7 public: 8 Outer () {m_outerInt = 0;} 9 void DisplayOut () {cout <m_outerInt <endl;} 10 friend Inner; 11}; 12 13 class Inner14 {15 private: 16 int m_innerInt; 17 public: 18 Inner () {m_innerInt = 1;} 19 void DisplayIn () {cout <m_innerInt <endl;} 20 // function 21 void TestFriend (Outer out) affected by youyuan) 22 {23 cout <"Good Friend:" <out. m_outerInt <endl; 24} 25}; 26 27 int main () 28 {29 Outer out; 30 out. displayOut (); 31 Inner in; 32 in. displayIn (); 33 in. testFriend (out); 34 return 0; 35}

After the preceding descriptions, all member functions of the class Inner are Outer-class friends functions, which can access private and protected members of the class Outer.

The purpose of the object meta declaration is to allow non-member functions or classes of the class to access private and protected members of the class objects. Note the following when using the object:
(1) Friendship cannot be inherited.

(2) The relationship between friends and friends is unidirectional and not interchangeable. If Class B is A friend of Class A, Class A is not necessarily A friend of class B. It depends on whether there is A corresponding declaration in the class.

(3) The relationship between friends and friends is not transmitted. If Class B is A friend of Class A, Class C is A friend of Class B, and class C is not necessarily A friend of Class A, it also depends on whether there is A corresponding statement in the class

(4) The youyuan function is not a member function of the class. Therefore, class: function name cannot be added to the class definition.

(5) You functions cannot directly access private members in the category through class objects,

Class A {int a; pulbic: friend void g ();}

Void g () {a = 2;} is incorrect.

Void g () {A m; m. a = 2;} is correct.

(6) The location where friend appears does not matter to friends. That is, the user meta statement is the same in both public private and protected locations.

 

If the internal class wants to achieve the user meta access effect (access non-public members of the instance directly through the instance or instance pointer ),It does not need to be declared as friend any more. The reason is self-evident: they are all themselves.

Q: How do I access members of an internal class instance (as a data member of an external class?

See the following code:

1 # include <iostream> 2 # define METHOD_PROLOGUE (theClass, localClass) \ 3 theClass * pThis = (theClass *) (char *) (this) -\ 4 offsetof (theClass, m_x # localClass); \ 5 6 using namespace std; 7 8 class Outer 9 {10 private: 11 int m_outerInt; 12 public: 13 Outer () {m_outerInt = 0;} 14 // internal class definition starts 15 class Inner16 {17 private: 18 int m_innerInt; 19 public: 20 Inner () {m_innerInt = 1;} 21 22 void DisplayIn () {cout <m_innerInt <endl ;}23 // access the data of the external class instance in this function 24 void setOut () 25 {26 METHOD_PROLOGUE (Outer, Inner); 27 pThis-> m_outerInt = 10; 28} 29} m_xInner; 30 // End internal class 31 32 void DisplayOut () {cout <m_outerInt <endl ;}33}; 34 35 int main () 36 {37 Outer out; 38 out. displayOut (); 39 out. m_xInner.setOut (); 40 out. displayOut (); 41 return 0; 42}

 

Look at the main function: after the program executes the first sentence of the main function, there will be a data block in the memory, which stores out data, and m_xInner is also in the data block. Of course, both the out and this pointers (external classes) point to the starting position of the memory block, and the this pointer in the internal class code points to the starting memory of m_xInner, offsetof (theClass, m_x # localClass) is the distance (offset) between m_xInner and the starting address (which is the out ADDRESS) of the memory block in the memory block ), that is, the difference value between the internal class this-External class this (in bytes). In this way, you can use the internal class this to subtract its own offset to get pThis. With the out address, you can basically do whatever you want. As to why should there be a char * strong conversion, you can go to definition of offsetof, we can see that there is a char conversion in its implementation.

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.