Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 继承范围方面的混淆_C++_Oop_Inheritance - Fatal编程技术网

C++ 继承范围方面的混淆

C++ 继承范围方面的混淆,c++,oop,inheritance,C++,Oop,Inheritance,这是我的密码: class Base{ public: getValue(int a){ x = a; } protected: int x; }; class Derived: public Base{ public: Derived():Base(){ Value = 0; } void Function(); } 因此,我的困惑来自继承,当您必须在派生函数中设置Base函数的

这是我的密码:

class Base{
    public:
    getValue(int a){
        x = a;
    }
    protected:
        int x;
    };

class Derived: public Base{
public:
    Derived():Base(){
        Value = 0;
    }
    void Function();
}
因此,我的困惑来自继承,当您必须在派生函数中设置
Base
函数的范围时,在google中,它说基类中的所有内容都是公共的。如果是这样的话,
Base
函数中的受保护值在
Derived
函数中不容易访问吗?那不是很糟糕吗


如果是这种情况,那么就没有办法在派生类中保护基类的受保护值

公共继承并不意味着一切都是公共的。这意味着从
Base
公开的内容在
Derived
中公开。受保护的数据成员未公开

如果您使用了受保护的继承,那么
Base
的公共方法将在
Derived
中受到保护。如果您使用私有继承,那么
Base
中的公共和受保护方法在
派生的
中将是私有的

我猜您可以说继承封装指示基类的最大可见性,而不是它的绝对可见性

编辑:实际上WhiZTiM提供的链接很好地解释了这一点:

“。在谷歌,它说基类中的所有内容都是公共的……”这是错误的。不要总是信任谷歌,C++上有太多错误的东西。顺便说一句,它不叫“范围”,“范围”是关于其他东西的。你的主题是