C++访问说明符
我只是想确保我了解公共权利和私人权利的概念。
关于私有访问说明符,是否意味着:
- 仅在类内部访问
- 不能从类的对象中访问,除非有可用于访问它们的公共类方法(可以其他对象使用那些公共函数?)
- 没有其他对象可以访问它们
并且对于公共:
- 从类的对象访问
- 从任何其他对象访问
是这样吗?
I just want to make sure I got the idea of public and private right.
Regarding the private access specifier, does it mean:
- Only accessed inside the class
- Cannot be accessed from the object of the class unless there are public class methods that can be used to access them (Can other objects use those public functions?)
- No other object can access them
And for public:
- Accessed from the object of the class
- Accessed from any other object
Is that right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
private :只有成员函数和类的友元可以访问它。
public :可以在对象具有范围的任何地方访问。
回答问题 -
私人:
public:
private : Only member functions and friends of class can access it.
public : Can be accessed anywhere where the object has scope.
Answering the questions -
private:
public:
我认为首先是词汇量的问题。
在 C++(以及大多数语言)中,
类
是一种类型
。您可以将其视为实际构建某些东西的蓝图。对象
是通过实际实例化类来生成的,即构建蓝图描述了什么。它或多或少是一组属性。您可以拥有同一类的多个对象,就像您可以拥有来自同一蓝图的多个房屋一样:请注意,它们的物理位置由于明显的原因而不同:)现在,谈谈可访问性。有 3 个典型的可访问级别:
public
、protected
和private
。public
意味着每个人都可以访问属性或方法protected
有点不那么简单。这意味着只有对象或其子对象可以访问属性(坏主意*)或方法。 (另外,在 C++ 中,friend
)private
表示只有该类的对象(而不是其子对象)可以访问属性或方法(另外,在 C++ 中,friend
s)注意:无论可访问性级别如何,对象都可以无限制访问同一类的任何对象的所有属性和方法。
(*) 尽管它时不时地弹出,但使用
protected
属性通常是一个坏主意。封装的目的是隐藏细节,不仅是为了隐藏细节,而且是因为通过精确控制谁可以访问数据,我们可以确保类保持其不变性(简单的例子,一个数组,其中您将单独存储大小,您需要确保“大小”始终真正代表数组中的项目数)。 注意:当您可以密封层次结构(例如在 C# 中)时,此限制不适用。I think there is an issue of vocabulary to begin with.
In C++ (and most languages) a
class
is atype
. You can think about it as a blueprint to actually build something.An
object
is produced by actually instantiating a class, that is, building what the blueprint described. It is more or a less a bundle of attributes. You can have several objects of the same class as you can have several houses from the same blueprint: note that their physical location is different for obvious reasons :)Now, on to the accessibility. There are 3 typical levels of accessibility:
public
,protected
andprivate
.public
, as expected, means that everyone is given access to either attributes or methodsprotected
is somewhat less trivial. It means that only the object, or its children, may access the attributes (bad idea*) or methods. (Plus, in C++,friend
s)private
means that only the objects of that class (and not their children) may access the attributes or methods (Plus, in C++,friend
s)Note: whatever the level of accessibility, an object has unrestricted access to all the attributes and methods of any object of the same class.
(*) Even though it pops up now and there, it is generally a bad idea to use
protected
attributes. The point of encapsulation is to hide the details, not only for the sake of it, but because by precisely controlling who can access the data, we can ensure that the class maintains its invariants (simple example, an array where you would store the size separately, you need to ensure that the "size" really represents the number of items in the array, at all times). Note: this restriction does not apply when you can seal a hierarchy, like in C# for example.私有成员只能由同一类的成员函数和静态函数以及该类的友元访问。在哪个对象上调用该函数并不重要。所以这个案子
是完全合法的。
Private members can only be accessed by member functions and static functions of the same class and by friends of the class. It does not matter on which object that function is called. So the case
is perfectly legal.
这似乎是正确的。任何人都可以从任何地方访问标记为公共的数据成员和函数。标记为私有的数据成员和函数只能由该类及其友元访问。但是,类的成员函数可以使用任何访问说明符访问数据,因此公共函数可以读写私有数据成员(这在 OOP 中普遍使用)。
That seems correct. Data members and functions marked public can be accessed from anywhere by anyone. Data members and functions marked private can only be accessed by the class and its friends. However, a member function of a class can access data with any access specifier, so a public function can read and write private data members (this is used universally in OOP).
在c++中,data和fn被封装为1个单元。
我们通过编写开始一个程序
预处理器指令
然后,类声明
接下来是 function(fn) 声明,我们还指定访问修饰符(public、private 或 protected)
&最后是main()程序。
如果我们声明一个 fn
私有:类的对象中的数据只能由其中定义的 fn 访问 - (具有数据和私有 fn 的对象)
公共:数据可以由任何 fn 访问
受保护:与私有类似,但数据也可以可以被继承另一个类的属性的子类访问。
例如,如果类 A 继承自类 B,则 A 是 B 的子类。
In c++ data and fn are encapsulated as 1 unit.
We begin a program by writing
preprocessor directives
Then, class declaration
Followed by function(fn) declaration where we also specify the access modifier ( public, private or protected)
& finally the main () program.
If we declare a fn
Private:the data within an object of a class is only accessed by fn defined within it- (the object which has the data and the private fn)
Public:the data can be accessed by any fn
Protected:similar to private however data can also be accessed by sub-classes that inherit the properties of another class.
Example if class A inherits from class B, thenA is a subclass of B.