c++ - C++中类名的访问修饰符

标签 c++ inheritance

我在 http://www.geeksforgeeks.org/inheritance-in-c/ 中查看继承概念.我对作者写的几句话感到困惑。作者在一处说

If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class .......

这意味着我们在 C++ 中有类似公共(public)类的东西吗?文章下表还表明存在公共(public)/ protected 类的概念。 enter image description here

我查看了其他一些 SO 帖子 ( Use of "Public" in a derived class declaration? ),没有发现对 Public、Private 或 protected 类本身的引用。本帖https://stackoverflow.com/questions/4792614/making-classes-public-to-other-classes-in-c公开的谈话,但通过头文件。

最佳答案

PublicProtectedPrivate 关键字是C++ 中的可见性标签。 c++ 中没有publicprotectedprivate 类类型(如Java ).这三个关键字也用在完全不同的上下文中来指定可见性继承模型。

下表列出了组件声明和继承模型的所有可能组合,展示了当子类被完全定义时对组件的访问结果。

enter image description here

它是这样写的(看第一行):

if a component is declared as public and its class is inherited as public the resulting access is public.

请看下面的示例:

class Super {
    private:     int x;
    protected:   int y;
    public:      int z;
 };
class Sub : protected Super {};

在类 Sub 中对变量 y、z 的最终访问是 protected ,而对变量 x 是无访问的。

注意:

缺少修饰符会产生 private

关于c++ - C++中类名的访问修饰符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46411484/

相关文章:

c++ - C++中装饰器模式的多态应用与耦合问题

c++ - 嵌套的 std::function

python - 如何将一个函数的重写一般地应用于 python 中的多个类?

javascript - 关于原型(prototype)继承的一个令人困惑的案例

java - 生成器模式和继承

c++ - 为 "external use"指定任意类类型的字段

c++ - 相当于Boost.Format的C++ 11

c++ - Tensorflow C++ 快速张量深拷贝

python - 在使用与异常相关的继承时,如何减少代码重复?

java - 类转换异常 : Extending Log4j Logger