Why copy constructors in C + + can tune private variables in other objects

Source: Internet
Author: User

The concept of private member variables, in the mind of the phenomenon is, with the Private keyword declaration, is the implementation of the class part, is not public, cannot access the object's private member variables outside the object.

However, when implementing the copy constructor and the assignment function, the object directly accesses the private member variable in the function, thus creating confusion. The following is a description of the specific example:

Wondering: Why lines 26th and 32nd lines can be compiled, and lines 39th and 40th will generate compilation errors?

1ClassCTest {2Public:3 CTest (Inti);4 CTest (Const ctest&RHS);5 ctest&Operator= (Const ctest&RHS);6void Printctest (Const ctest&RHS);7Private:8IntValue9};10Ctest::ctest (Inti): Value (i)12{cout<<"Contructor of CTest"<<Endl14}15Ctest::ctest (Const ctest&RHS): Value (Rhs.value)17{cout<<"Copy Contructor of CTest"<<Endl19}20ctest& CTest::Operator= (Const ctest&Rhs22{cout<<"Assign function of CTest"<<Endl24Ifthis = = &Rhs25return *This;value = Rhs.value;//Accessing private member variables through objects27return *This;28}2930void CTest::p rintctest (Const ctest&Rhs31{cout<<rhs.value<<endl;//Accessing private member variables through objects33}3435Int main () 36 {37 ctest t = 1; CTest TT = 2; // cout<<t.value<<endl; // Access private member variables by object, compile error 40 // cout<<tt.value<<endl; // Access private member variables by object, compile error 41 Span style= "COLOR: #000000" > t.printctest (TT); 42}             

The reason for this confusion is that your own understanding of private member variables is incorrect, encapsulation is the concept of the compile period, and is for types rather than objects, the private member variables of the same type instance object can be accessed in the member functions of the class.

The specific parsing is as follows: The symbol of the value from the variable is how parsing is parsed.

1. Determining the lookup domain for a symbol

As the 26th line of code, when the compiler discovers the value variable, it looks for the symbol in the class field of the object that the value variable belongs to RHS.

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

As the 1th step shows, the current lookup domain is a class domain, and the Printctest function is in the CTest class body, so printctest can access all variables in the CTest class (including private member variables), so the value symbol is found in the CTest class domain.

As the 39th line of code, the main function is not in the CTest class body, so the main function does not have access to private member variables in the CTest class domain.

3. Symbol has been found, compiled by

The access rights of a class member variable are imposed by the compiler, and the compiler can find value, and by compiling it, it is natural to have access to the values of the value variable.

Intuitively, we would assume that the lookup field of the value symbol in line 26th is the scope of the object RHS, whereas the implementation of the C + + compiler looks for the value symbol in the class domain of the object RHS.

Inspiration: Some intuition is unreliable and requires deep analysis of the underlying principles of implementation before they can be understood thoroughly.

Why copy constructors in C + + can tune private variables in other 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.