使用私有修饰符,为什么可以直接访问其他对象中的成员?

时间:2022-09-25 14:47:57

I have the following code:

我有以下代码:

class A 
{
private:
    int x;
public:
    A()
    {
        x = 90;
    }
    A(A a1, A a2)
    {
        a1.x = 10;
        a2.x = 20;
    }
    int getX()
    {
        return this->x;
    }
};

I know that code might be weird but I don't understand why a1 and a2 can access private data member x?

我知道代码可能很奇怪,但我不明白为什么a1和a2可以访问私有数据成员x?

4 个解决方案

#1


24  

Good question. The point is that protection in C++ is class level, not object level. So a method being invoked on one object can access private members of any other instance of the same class.

好问题。关键是C ++中的保护是类级别,而不是对象级别。因此,在一个对象上调用的方法可以访问同一个类的任何其他实例的私有成员。

This makes sense if you see the role of protection being to allow encapsulation to ensure that the writer of a class can build a cohesive class, and not have to protect against external code modifying object contents.

如果你看到保护的作用是允许封装以确保类的编写者可以构建一个内聚类,而不必防止外部代码修改对象内容,这是有道理的。

Another thought as to the real "why?". Consider how you write almost any copy constructor; you want access to the original's underlying data structure, not its presented interface.

另一个想法是真正的“为什么?”。考虑如何编写几乎任何复制构造函数;您希望访问原始的底层数据结构,而不是其呈现的界面。

#2


4  

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).

在这种情况下,它是构造函数,它是其他实例(即a1,a2)。

#3


0  

Short answer: In member methods of class A, all the members of (object/pointer and static member) class A can be accessed.

简答:在A类的成员方法中,可以访问(对象/指针和静态成员)类A的所有成员。

#4


0  

A(A a1, A a2)
{
    a1.x = 10;
    a2.x = 20;
}

Now from my understanding, you question is how can a object that invoked the constructor call can access other class member variables ?

现在从我的理解,你的问题是如何调用构造函数调用的对象可以访问其他类成员变量?

Now, both the constructor and the arguments a1,a2 are class scoped. So, it can access all it's members irrespective of it's access level. This too will also work in the constructor -

现在,构造函数和参数a1,a2都是类作用域。因此,无论访问级别如何,它都可以访问所有成员。这也可以在构造函数中工作 -

this->x = a1.x; // Notice that "this" members can be accessed too.
                // How ever both the member variables are different and are part of
                // different objects.

#1


24  

Good question. The point is that protection in C++ is class level, not object level. So a method being invoked on one object can access private members of any other instance of the same class.

好问题。关键是C ++中的保护是类级别,而不是对象级别。因此,在一个对象上调用的方法可以访问同一个类的任何其他实例的私有成员。

This makes sense if you see the role of protection being to allow encapsulation to ensure that the writer of a class can build a cohesive class, and not have to protect against external code modifying object contents.

如果你看到保护的作用是允许封装以确保类的编写者可以构建一个内聚类,而不必防止外部代码修改对象内容,这是有道理的。

Another thought as to the real "why?". Consider how you write almost any copy constructor; you want access to the original's underlying data structure, not its presented interface.

另一个想法是真正的“为什么?”。考虑如何编写几乎任何复制构造函数;您希望访问原始的底层数据结构,而不是其呈现的界面。

#2


4  

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).

在这种情况下,它是构造函数,它是其他实例(即a1,a2)。

#3


0  

Short answer: In member methods of class A, all the members of (object/pointer and static member) class A can be accessed.

简答:在A类的成员方法中,可以访问(对象/指针和静态成员)类A的所有成员。

#4


0  

A(A a1, A a2)
{
    a1.x = 10;
    a2.x = 20;
}

Now from my understanding, you question is how can a object that invoked the constructor call can access other class member variables ?

现在从我的理解,你的问题是如何调用构造函数调用的对象可以访问其他类成员变量?

Now, both the constructor and the arguments a1,a2 are class scoped. So, it can access all it's members irrespective of it's access level. This too will also work in the constructor -

现在,构造函数和参数a1,a2都是类作用域。因此,无论访问级别如何,它都可以访问所有成员。这也可以在构造函数中工作 -

this->x = a1.x; // Notice that "this" members can be accessed too.
                // How ever both the member variables are different and are part of
                // different objects.