With a private modifier, why can the member in other objects be accessed directly?

前端 未结 4 1983
梦如初夏
梦如初夏 2020-11-28 08:13

I have the following code:

class A 
{
private:
    int x;
public:
    A()
    {
        x = 90;
    }
    A(A a1, A a2)
    {
        a1.x = 10;
        a2.x         


        
4条回答
  •  盖世英雄少女心
    2020-11-28 08:38

    Any member function of the class as well as the constructors can access the private data. That is the private members of the instance object the method is called on or the private members of other instances.

    In this case it's the constructor and it's other instances (namely a1, a2).

提交回复
热议问题