C ++做子类真正继承私有成员变量吗? [英] C++ Do Sub-Classes Really Inherit Private Member Variables?

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

问题描述

好的,我在这里有一个有趣的问题/问题。这将需要我解释一下我知道的继承到目前为止那么光明与我,我仍在学习。

Alright, I have an interesting question/problem here. This will kind of require me to explain what I know of inheritance so far so bare with me, I'm still learning.

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

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

现在,理论上基于我的想法, $ b 15
15
由于它总是使用myClass版本的函数(因此使用myClass myVariable)。但奇怪的是,情况并非如此。运行这个程序的结果打印:
15
0
这让我怀疑,我们实际上不是继承myVariable,但是我们也有能力搞砸它?显然,这是创建一个替代版本的myVariable不知何故,否则不会有一个0的myClass版本。我们正在通过这样做编辑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).

有一点混乱

回想一下,继承是为C和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.

如果我们以这种方式订购访问级别: public protected private ,从最少保护到最受保护,可以定义继承修饰符,将继承类字段的访问级别至少提高到它们在派生类(即类继承)中指定的级别。

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 继承 protected inheritance modifier:

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

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

,则 A 至少在 B 保护级别:


  • public 字段变为 protected public 级别提高到 protected

  • c $ c> protected (相同的访问级别,因此无需修改)

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

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

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