c++ - 子类真的继承私有(private)成员变量吗?

标签 c++ inheritance private members

据我所知,基本上,当您创建具有公共(public)、 protected 和私有(private)部分以及每个公共(public)和 protected 部分中的变量/函数的基类时,将继承到子类的适当部分(定义按类子类:私有(private)基类,它将所有基类的公共(public)和 protected 成员置于公共(public)状态,将“私有(private)”一词更改为公共(public)将它们全部置于公共(public)状态并将其更改为 protected 将它们全部置于 protected 状态。

所以,当你创建一个子类时,你永远不会从前一个类(在这种情况下是基类)的私有(private)部分收到任何东西,如果这是真的,那么子类的对象永远不应该拥有它自己的基类中的私有(private)变量或函数的版本是否正确?

我们来看一个例子:

#include <iostream>

class myClass     // Creates a class titled myClass with a public section and a private section.
{
public:
  void setMyVariable();
  int getMyVariable();
private:
  int myVariable;     // This private member variable should never be inherited.
};

class yourClass : public myClass {};    // Creates a sub-class of myClass that inherits all the public/protected members into  the
// public section of yourClass. This should only inherit setMyVariable()
// and getMyVariable() since myVariable is private. This class does not over-ride any
// functions so it should be using the myClass version upon each call using a yourClass
// object. Correct?

int main()
{
  myClass myObject;           // Creates a myClass object called myObject.
  yourClass yourObject;       // Creates a yourClass object called yourObject
  yourObject.setMyVariable(); // Calls setMyVariable() through yourObject. This in turn calls the myClass version of it    because
  // there is no function definition for a yourClass version of this function. This means that this
  // can indeed access myVariable, but only the myClass version of it (there isn't a yourClass
  // version because myVariable is never inherited).

  std::cout << yourObject.getMyVariable() << std::endl;   // Uses the yourClass version of getMyVariable() which in turn
  // calls the myClass version, thus it returns the myClass myVariable
  // value. yourClass never has a version of myVariable Correct?

  std::cout << myObject.getMyVariable() << std::endl;     // Calls the myClass version of getMyVariable() and prints myVariable.

  return 0;
}

void myClass::setMyVariable()
{
  myVariable = 15;        // Sets myVariable in myClass to 15.
}

int myClass::getMyVariable()
{
  return myVariable;      // Returns myVariable from myClass.
}

现在,根据我的想法,理论上应该打印: 15 15 由于它总是使用函数的 myClass 版本(因此使用 myClass myVariable)。但是,奇怪的是,事实并非如此。运行此程序的结果打印: 15 0 这让我想知道,我们真的不仅继承了 myVariable,而且我们也有能力乱搞它吗?显然,这是在以某种方式创建 myVariable 的替代版本,否则 myClass 版本不会有 0。通过执行所有这些操作,我们确实在编辑 myVariable 的第二个拷贝。

谁能给我解释一下这一切,这打破了我对继承的理解。

最佳答案

Basically as far as I know, when you create a base class with a public, protected, and private section and variables/functions in each the public and protected sections will get inherited into the appropriate section of the sub-class (defined by class subclass : private base, which will take all public and private members of base and put them into public, changing the word private to public puts them all in public and changing it to protected puts them all into protected).

这个说法有点困惑。

回想一下,继承是为 C++ 中的类和结构定义的。单个对象(即实例)不会从其他对象继承。使用其他对象构造一个对象称为组合

当一个类从另一个类继承时,它会从该类获取所有内容,但被继承字段的访问级别可能会禁止它们在继承者中的使用。

此外,类有 3 种继承方式:private(默认)、protectedpublic。当被子类继承时,它们中的每一个都会改变类属性和方法的访问级别。

如果我们以这种方式对访问级别进行排序:publicprotectedprivate,从 protected 程度最低到 protected 程度最高,那么我们可以将继承修饰符定义为将继承类字段的访问级别至少提升到派生类中指定的级别(即类继承)。

例如,如果类 B 继承自类 A 并带有 protected 继承修饰符:

  class B : protected A { /* ... */ };

那么 A 中的所有字段将至少具有 B 中的 protected 级别:

  • public 字段变为 protected(public 级别提升为 protected),
  • protected 字段保持protected(访问级别相同,因此此处无需修改),
  • private 字段保持private(访问级别已经在修饰符之上)

关于c++ - 子类真的继承私有(private)成员变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14270631/

相关文章:

c++ - 处理 Github 存储库中的外部 C/C++ 代码库

c++ - 模板练习问题

c++ - 将符号应用于 16 位的 dicom 图像

java - 导入包会改变类的可见性吗?

android - 通过私有(private)地址访问网站

android - 是否可以在 Android Studio 中调试 C/C++?

css - 更改现有 CSS 阴影的方向

具有抽象参数和继承的Java抽象方法

c++ - 派生类通过基类定义函数

python - 如何在 Python 中使私有(private)变量不可访问?