C++ 为什么同一个类的对象可以访问彼此的私有数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6921185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do objects of the same class have access to each other's private data?
提问by Keith
Why do objects of the same class have access to each other's private data?
为什么同一个类的对象可以访问彼此的私有数据?
class TrivialClass {
public:
TrivialClass(const std::string& data) :
mData(data) {};
const std::string& getData(const TrivialClass& rhs) const {
return rhs.mData;
};
private:
std::string mData;
};
int main() {
TrivialClass a("fish");
TrivialClass b("heads");
std::cout << "b via a = " << a.getData(b) << std::endl;
return 0;
}
This codes works. It is perfectly possible for object a to access private data from object b and return it. Why should this be so? I would think that private data is private. (I started out by trying to understand copy constructors in the pimpl idiom, but then I discovered that I didn't even understand this simple situation.)
此代码有效。对象 a 完全有可能从对象 b 访问私有数据并返回它。为什么会这样?我认为私人数据是私人的。(我一开始试图理解 pimpl 成语中的复制构造函数,但后来我发现我什至不理解这个简单的情况。)
回答by AnT
Because that's how it works in C++. In C++ access control works on per-classbasis, not on per-object basis.
因为这就是它在 C++ 中的工作方式。在 C++ 中,访问控制基于每个类,而不是基于每个对象。
Access control in C++ is implemented as a static, compile-time feature. I think it is rather obvious that it is not really possible to implement any meaningful per-object access control at compile time. Only per-class control can be implemented that way.
C++ 中的访问控制是作为静态的编译时功能实现的。我认为很明显,在编译时实现任何有意义的每个对象访问控制是不可能的。只能以这种方式实现每类控制。
Some hints of per-object control are present in protected accessspecification, which is why it even has its own dedicated chapter in the standard (11.5). But still any per-object features described there are rather rudimentary. Again, access control in C++ is meant to work on per-class basis.
受保护的访问规范中存在一些针对每个对象的控制提示,这就是为什么它甚至在标准 (11.5) 中有自己的专门章节。但是那里描述的任何每个对象的功能仍然相当基本。同样,C++ 中的访问控制旨在针对每个类工作。
回答by vsekhar
"Private" isn't really an access controlmechanism in the sense of "I made my pictures on facebook private so you can't see them."
“私人”并不是真正意义上的访问控制机制,即“我将我在 facebook 上的照片设为私有,因此您无法看到它们”。
In C++, "private" simply says these are parts of a class that you (the coder of the class) might change in future versions, etc., and you don't want other coders using your class to rely on their existence or functionality.
在 C++ 中,“私有”只是说这些是您(类的编码人员)可能会在未来版本等中更改的类的一部分,并且您不希望其他使用您的类的编码人员依赖它们的存在或功能.
If you want true access control, you should implement genuine data security techniques.
如果您想要真正的访问控制,您应该实施真正的数据安全技术。
回答by HymanyZhu
This is a good question and I have come across this question recently. I had some discussions with my colleagues and here is the summary of our discussion: This is by design. It doesn't mean this design is totally reasonable for all cases, but there must be some considerations why per class private is chosen. The possible reasons we could think of include:
这是一个很好的问题,我最近遇到了这个问题。我与我的同事进行了一些讨论,以下是我们讨论的摘要:这是设计使然。这并不意味着这种设计对所有情况都是完全合理的,但必须有一些考虑为什么选择每个类私有。我们能想到的可能原因包括:
First of all, the cost of per instance access control could be very high. This has been discussed by others in this thread. In theory, this can be done via thispointer check. However, this cannot be done at compilation time, and can only be done at run time. So you have to identify the access control of each member at run time, and when it's violated possibly only exceptions will be raised. The cost is high.
首先,每个实例访问控制的成本可能非常高。其他人已在此线程中讨论过此问题。理论上,这可以通过这个指针检查来完成。但是,这不能在编译时完成,只能在运行时完成。所以你必须在运行时识别每个成员的访问控制,当它被违反时,可能只会引发异常。成本很高。
Secondly, per class access control has its own use case, like copy constructor or operator =. It would be difficult to implement them if access control is per instance.
其次,每个类的访问控制都有自己的用例,比如复制构造函数或运算符 =。如果访问控制是每个实例,则很难实现它们。
Plus, the access control is mainly from programming/language perspective, for how to modularise/control the access to the code/member, not the data.
另外,访问控制主要是从编程/语言的角度来看,如何模块化/控制对代码/成员的访问,而不是数据。
回答by André Caron
It's somewhat of an arbitrary language design decision. In Ruby, for instance, private
really means private, as in "only the instance can access its own private data members". However, this is somewhat restrictive.
这在某种程度上是一种任意的语言设计决定。例如,在Ruby 中,private
真正意味着私有,就像“只有实例可以访问它自己的私有数据成员”一样。然而,这有点限制。
As pointed in the comments, copy constructors and assignment operators are common places where you access another instance's private data members directly. There are less obvious reasons why.
正如评论中所指出的,复制构造函数和赋值运算符是您直接访问另一个实例的私有数据成员的常见位置。原因不太明显。
Consider the following case. You're implementing an OO linked-list. The linked-list has a nested node class for managing pointers. You might implement this node class such that it manages the pointers itself (rather than having the pointers public and managed by the list). In such a case, you'd have the node objects wanting to modify other node objects' pointers at other places that the typical copy constructor and assignment operator.
考虑以下情况。您正在实现一个面向对象的链表。链表有一个用于管理指针的嵌套节点类。您可以实现这个节点类,使其管理指针本身(而不是让指针公开并由列表管理)。在这种情况下,您会希望节点对象在典型的复制构造函数和赋值运算符的其他位置修改其他节点对象的指针。
回答by Adam Maras
The trick is to remember that the data is private
to the class, not the instanceof the class. Any method within your class can access the private data of any instance of that class; there's not a way to keep data private to within an instance unless you forbid methods that explicitly access private data members of other instances.
诀窍是记住数据是private
给类的,而不是类的实例。类中的任何方法都可以访问该类的任何实例的私有数据;除非您禁止显式访问其他实例的私有数据成员的方法,否则无法将数据保持在实例内私有。
回答by Jacob
In addition to all the answers above, consider custom copy constructors, assignment operators and all the other functions you would write for a class which operate on other instances. You would need accessor functions for all those data members.
除了上面的所有答案之外,请考虑自定义复制构造函数、赋值运算符以及您为在其他实例上运行的类编写的所有其他函数。您将需要所有这些数据成员的访问器函数。
回答by YeenFei
Private data remains private until somebody who has access to it reveal it to other.
私有数据将保持私有,直到有权访问它的人将其透露给其他人。
This concept applies to other situation too, such as :
这个概念也适用于其他情况,例如:
class cMyClass
{
public:
// ...
// omitted for clarity
// ...
void Withdraw(int iAmount)
{
iTheSecretVault -= iAmount;
}
private:
int iTheSecretVault;
};
How could anyone withdraw the money ? :)
怎么会有人取钱?:)