C ++ private member variables

Source: Internet
Author: User

The concept of private member variables in my mind is that it is declared with the private keyword, which is the implementation part of the class. It is not made public and cannot access the private member variables of the object outside the object.

However, when the copy constructor and the value assignment function are implemented, the private member variables are directly accessed using the object in the function, resulting in confusion. The following is an example:

 1   Class  Ctest {  2   Public  :  3 Ctest ( Int  I );  4 Ctest ( Const Ctest & RHS );  5 Ctest &Operator = ( Const Ctest & RHS );  6       Void Printctest ( Const Ctest & RHS );  7   Private  :  8       Int  Value;  9   };  10  11 Ctest: ctest ( Int  I): Value (I)  12   {  13 Cout < "  Contructor of ctest  " < Endl;  14   }  15   16 Ctest: ctest ( Const Ctest &RHS): Value (RHS. value)  17   {  18 Cout < "  Copy contructor of ctest  " < Endl;  19   }  20   21 Ctest & ctest :: Operator = ( Const Ctest & RHS) 22   {  23 Cout < "  Assign function of ctest  " < Endl;  24   }  25   26 Ctest & ctest :: Operator = ( Const Ctest & RHS)  27  {  28 Cout < "  Assign function of ctest  " < Endl;  29       If ( This == & RHS)  30           Return * This  ;  31 Value = RHS. value;//  Access private member variables through objects  32       Return * This  ;  33   }  34   35   Void Ctest: printctest ( Const Ctest & RHS)  36   {  37 Cout <RHS. value <Endl; //  Access private member variables through objects  38   }  39   Int  Main ()  40   {  41 Ctest T = 1  ;  42 Ctest TT = 2  ;  43  //  Cout <t. value <Endl;  //  Accessing private member variables through an object causes compilation errors  44   //  Cout <TT. value <Endl;  //  Accessing private member variables through an object causes compilation errors  45   T. printctest (TT );  46 }

Doubt:Why 31st rows and 37th rowsCodeCan the code be compiled, but will the code of lines 43rd and 44th generate a compilation error?

The reason for this is that you have incorrect understanding of Private member variables,Encapsulation is a concept during the compilation period. It targets types rather than objects. You can access private member variables of objects of the same type in member functions of the class..

The specific analysis is as follows:How to parse the symbol from the variable value.

1. Determine the search domain of the symbol

For example, if the compiler finds the value variable, it searches for the symbol in the class field of The RHS object to which the value variable belongs.

2. Determine which symbols in the current domain can be accessed.

Step 1 shows that the currently searched domain is a class domain, while the printctest function is in the ctest class body. Therefore, printctest can access all variables (including private member variables) in the ctest class ), therefore, the value symbol is found in the ctest class.

For example, if the code is 43rd lines, the main function is not in the ctest class, so the main function cannot access the private member variable in the ctest class.

3. the symbol has been found and compiled

The access permission of class member variables is imposed by the compiler. the compiler can find the value. By compiling, the value of the value variable can naturally be accessed.

Intuitively, we will assume that the value search domain in the 37th line of code should be the corresponding scope of the object RHS. However, the implementation of the C ++ compiler is to find the value symbol in the class domain of the object RHS.

Inspiration: Some intuition is unreliable. You need to thoroughly analyze the implementation principles behind it before you can fully understand it.

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.