Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the scope of private in C++?

Shulou Source: shulou.com Published: 2022-06-02 09:24:52 09月24日 Update

Today, I would like to talk to you about the scope of private in C++, which many people may not quite understand. In order to make you understand better, the editor summarized the following content for you. I hope you can get something from this article.

Why variables declared as private in a class can be called directly by the copy constructor in this class and can also be compiled through.

As a C++ engineer, I haven't thought about this problem carefully all the time, and recently I suddenly realized that this is a very interesting question. Simply do the next verification, the original we usually mentioned public, protected, private decorated with class, not the corresponding instance of class.

That is to say, as long as the function of private is in class, it can be called. As for why it cannot be called directly by the instance after instantiation, it is not because the instance cannot be called, but because the instantiated operation is not within this class, and the scope is outside the class, which leads to the variable that cannot be called private directly.

In order to confirm this conclusion, the following program is specially done, and the Test (Apoca) function is deliberately implemented, and it is found that there is no problem with the compilation.

Examples are as follows:

The copy constructor and the Test () function are both private members that call the input parameter directly, so there is no problem with Gateway + compilation.

The running result is as follows:

After reading the above, do you have any further understanding of the scope of private in C++? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Function instance problem scope Category content compilation Action variable is in result interesting that is example cause Engineering engineer consciousness member Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Docker Microsoft OPPO Reno