子类是否真的继承了私有成员变量? [英] Do Sub-Classes Really Inherit Private Member Variables?

查看:154
本文介绍了子类是否真的继承了私有成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 protected 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).

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

So, when you create a sub-class you never receive anything from the private section of the previous class (the base class in this case), if this is true then an object of the sub-class should never have it's own version of a private variable or function from the base class correct?

让我们来看一个例子:

#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的第二个副本。

Now, in theory based on what I think, this should print: 15 15 Due to it simply always using the myClass version of the functions (thus using the myClass myVariable). But, strangely, this is not the case. The result of running this program prints: 15 0 This makes me wonder, are we actually not only inheriting myVariable, but do we also have the ability to mess around with it? Clearly this is creating an alternate version of myVariable somehow otherwise there wouldn't be a 0 for the myClass version. We are indeed editing a second copy of myVariable by doing all this.

有人可以向我解释这一切,这已经撕裂了我对继承的理解。

Can someone please explain this all to me, this has torn apart my understanding of inheritance.

推荐答案


基本上据我所知,当你创建一个带有public,protected和private的基类时每个公共部分和受保护部分中的部分和变量/函数将被继承到子类的适当部分(由类子类定义:私有基础,它将占用基础的所有公共和私有成员并将它们公开,改变私人公开这个词将它们全部公开,并将其改为受保护,将它们全部保护起来。)

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

有一点混乱在本声明中。

There's a bit of confusion in this statement.

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

Recall that inheritance is defined for classes and structs in C++. Individual objects (ie. instances) do not inherit from other objects. Constructing an object using other objects is called composition.

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

When a class inherits from another class, it gets everything from that class, but the access level of the inherited fields may inhibit their use within the inheritor.

此外,类有3种继承: private (这是默认值), protected public 。当子类继承时,它们中的每一个都会更改类属性和方法的访问级别。

Furthermore, there are 3 kinds of inheritance for classes: private (which is the default), protected, and public. Each of them changes the access level of a class properties and methods when inherited by a subclass.

如果我们以这种方式命令访问级别:公共受保护私人,从受保护程度最低到最受保护,然后我们可以将继承修饰符定义为在派生类(即继承的类)中将继承的类字段的访问级别提升到至少它们指定的级别。

If we order the access levels in this manner: public, protected, private, from the least protected to the most protected, then we can define the inheritance modifiers as raising the access levels of the inherited class fields to at least the level they designate, in the derived class (ie. the class inheriting).

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

For instance, if class B inherits from class A with the protected inheritance modifier:

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

然后 A 中的所有字段都会有 B 中至少受保护的级别:

then all the fields from A will have at least the protected level in B:


  • public 字段变为 protected public 级别被提升到受保护),

  • 受保护字段保持 protected (相同的访问级别,因此不需要修改),

  • private 字段停留 private (访问级别已经高于修饰符)

  • public fields become protected (public level is raised to protected),
  • protected fields stay protected (same access level, so no modification here),
  • private fields stay private (the access level is already above the modifier)

这篇关于子类是否真的继承了私有成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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