C ++访问说明符 [英] C++ access specifiers

查看:112
本文介绍了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

对于公众:


  • 从对象的对象访问class

  • 从任何其他对象访问

对吗?

推荐答案

我认为开始存在词汇问题。

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

在C ++中(和大多数语言)的类型。您可以将其视为构建实际内容的蓝图。

In C++ (and most languages) a class is a type. 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 and private.


  • public ,这意味着每个人都可以使用属性或方法

  • 受保护的不太重要。这意味着只有对象或其子对象可以访问属性(不好的主意*)或方法。 (另外,在C ++中, friend s)

  • private 表示仅该类的对象(而不是其子对象)可以访问属性或方法(另外,在C ++中, friend s)

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

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

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

这篇关于C ++访问说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆