c – 子类是否真的继承了私有成员变量?

基本上据我所知,当你创建一个带有public,protected和private部分的基类以及每个public和protected部分中的变量/函数时,它们将被继承到子类的相应部分(由类子类定义) :私人基地,它将采取所有公共和受保护的基地成员并将其公开,将私人一词改为公开,将其全部公开,并将其更改为受保护,将其全部置于保护状态).

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

让我们来看一个例子:

#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(默认),protected和public.当子类继承时,它们中的每一个都会更改类属性和方法的访问级别.

如果我们以这种方式命令访问级别:public,protected,private,从受保护程度最低到受保护最多,那么我们可以将继承修饰符定义为将继承的类字段的访问级别提升到至少它们指定的级别,在派生类中(即继承的类).

例如,如果B类继承自带有受保护继承修饰符的A类:

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

那么A中的所有字段都至少具有B中的受保护级别:

>公共领域受到保护(公共级别被提升为受保护的),
>受保护的字段保持受保护(相同的访问级别,因此这里不做修改),
>私有字段保持私有(访问级别已经高于修饰符)

上一篇:redis中的数据读取


下一篇:Compilation Firewalls - PIMPL