Object can access its private member variables directly

Source: Internet
Author: User

Turn from:

Http://www.cnblogs.com/dwdxdy/archive/2012/07/17/2595741.html
Understanding of "C + +" Private member variables

The concept of a private member variable, in the mind of the phenomenon is, in the Private keyword declaration, is the implementation of the class part, not externally, can not be outside the object access to the object's private member variables.

However, in the implementation of the copy constructor and the assignment function, the object directly accesses the private member variables in the function, so the confusion arises. Here is a concrete example to illustrate:

Wonder: Why lines 26th and 32nd can be compiled, while lines 39th and 40th produce compile errors.

Class CTest {public
:
    ctest (int i); 
    CTest (const ctest& RHS);
    ctest& operator= (const ctest& RHS);
    void Printctest (const ctest& RHS);
Private:
    int value;
};

ctest::ctest (int i): value (i)
{
    cout<< "contructor of CTest" <<endl;
}

Ctest::ctest (const ctest& RHS): Value (Rhs.value)
{
    cout<< "Copy contructor of CTest" <<endl;
}

ctest& ctest::operator= (const ctest& RHS)
{
    cout<< "Assign function of CTest" << Endl;
    if (this = = &RHS) return
        *this;
    Value = Rhs.value;                Accessing private member variables via objects return
    *this;

void CTest::p rintctest (const ctest& RHS)
{
    cout<<rhs.value<<endl;        Access private member variable through object

{main ()
{
    CTest t = 1;
    CTest TT = 2;  cout<<t.value<<endl;        Accessing private member variables through objects, compiling errors
    //  cout<<tt.value<<endl;        To access a private member variable through an object, compile the error
    t.printctest (TT);
}

The reason for this confusion is that you have an incorrect understanding of a private member variable, that the encapsulation is a compile-time concept, is for a type, not an object, and that you can access the private member variables of the same type instance object in the member function of the class.

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

1. Identify the symbol of the lookup field

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

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

By the 1th step, the current lookup domain is a class domain, and the Printctest function is in the CTest class body, so printctest can access all the variables in the CTest class, including private member variables, so that 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 cannot access the private member variable in the CTest class domain.

3. Symbols have been found, compiled by

The access rights of class member variables are imposed by the compiler, the compiler can find value, and by compiling, the value of a value variable can be accessed naturally.

Intuitively, we would think that the lookup field of the value symbol in line 26th should be the scope of the object RHS, but the C + + compiler's implementation is to find the value symbol in the class domain of the object RHS.

Inspiration: Some intuition is unreliable, need to deeply analyze the implementation of the principle behind it can be understood thoroughly.

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.