C++访问说明符

发布于 2024-10-16 14:22:08 字数 264 浏览 11 评论 0原文

我只是想确保我了解公共权利和私人权利的概念。

关于私有访问说明符,是否意味着:

  • 在类内部访问
  • 不能从类的对象中访问,除非有可用于访问它们的公共类方法(可以其他对象使用那些公共函数?
  • 没有其他对象可以访问它们

并且对于公共:

  • 从类的对象访问
  • 从任何其他对象访问

是这样吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

高速公鹿 2024-10-23 14:22:08

private :只有成员函数和类的友元可以访问它。
public :可以在对象具有范围的任何地方访问。


回答问题 -

私人

  1. 的。 (其他对象可以使用这些公共函数吗?如果没有类关系,类的一个对象就无法与另一对象的成员进行通信。)
  2. 友元可以访问类的私有成员。所以,答案取决于你的班级是否有朋友。

public

  1. 取决于对象与您尝试访问的成员类是否具有层次关系。

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:

  1. Yes
  2. Yes. (Can other objects use those public functions? With out having class relations, one object of class cannot communicate to the other's members. )
  3. Friends has access to private members of a class. So, answer depends upon your class has friends or not.

public:

  1. Yes
  2. Depends whether the object has hierarchical relations to the member's class you are trying to access.
ぃ弥猫深巷。 2024-10-23 14:22:08

我认为首先是词汇量的问题。

在 C++(以及大多数语言)中, 是一种类型。您可以将其视为实际构建某些东西的蓝图。

  • 它描述了所持有的属性
  • 它描述了对这些属性进行操作的方法
  • 它描述了适用的限制:这就是“可访问性”

对象是通过实际实例化类来生成的,即构建蓝图描述了什么。它或多或少是一组属性。您可以拥有同一类的多个对象,就像您可以拥有来自同一蓝图的多个房屋一样:请注意,它们的物理位置由于明显的原因而不同:)

现在,谈谈可访问性。有 3 个典型的可访问级别:publicprotectedprivate

  • 正如预期的那样,public 意味着每个人都可以访问属性或方法
  • protected 有点不那么简单。这意味着只有对象或其子对象可以访问属性(坏主意*)或方法。 (另外,在 C++ 中,friend
  • private 表示只有该类的对象(而不是其子对象)可以访问属性或方法(另外,在 C++ 中, friends)

注意:无论可访问性级别如何,对象都可以无限制访问同一类的任何对象的所有属性和方法。

(*) 尽管它时不时地弹出,但使用 protected 属性通常是一个坏主意。封装的目的是隐藏细节,不仅是为了隐藏细节,而且是因为通过精确控制谁可以访问数据,我们可以确保类保持其不变性(简单的例子,一个数组,其中您将单独存储大小,您需要确保“大小”始终真正代表数组中的项目数)。 注意:当您可以密封层次结构(例如在 C# 中)时,此限制不适用。

I think there is an issue of vocabulary to begin with.

In C++ (and most languages) a class is a type. You can think about it as a blueprint to actually build something.

  • it describes the attributes that are held
  • it describes the methods to operate on those attributes
  • it describes the restrictions that apply: this is the "accessibility"

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 and private.

  • public, as expected, means that everyone is given access to either attributes or methods
  • protected is somewhat less trivial. It means that only the object, or its children, may access the attributes (bad idea*) or methods. (Plus, in C++, friends)
  • private means that only the objects of that class (and not their children) may access the attributes or methods (Plus, in C++, friends)

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.

一曲爱恨情仇 2024-10-23 14:22:08

私有成员只能由同一类的成员函数和静态函数以及该类的友元访问。在哪个对象上调用该函数并不重要。所以这个案子

class Foo
{
  private:
    void bar() {}
  public:
    void baz(Foo& var)
    {
      var.bar();
    }
}

是完全合法的。

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

class Foo
{
  private:
    void bar() {}
  public:
    void baz(Foo& var)
    {
      var.bar();
    }
}

is perfectly legal.

德意的啸 2024-10-23 14:22:08

这似乎是正确的。任何人都可以从任何地方访问标记为公共的数据成员和函数。标记为私有的数据成员和函数只能由该类及其友元访问。但是,类的成员函数可以使用任何访问说明符访问数据,因此公共函数可以读写私有数据成员(这在 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).

深府石板幽径 2024-10-23 14:22:08

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文