C++ 虚函数继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17842594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:29:18  来源:igfitidea点击:

Virtual function inheritance

c++classinheritancevirtual-functions

提问by Robert Lucian Chiriac

I have a confusion about the inheriting the virtual property of a method.

我对继承方法的虚拟属性感到困惑。

Let's suppose we have 4 classes: class A, class B, class C and class D. The classes are inherited by this way: A -> B -> C -> D, where A is the base class.

假设我们有 4 个类:A 类、B 类、C 类和 D 类。这些类是通过这种方式继承的:A -> B -> C -> D,其中 A 是基类。

By this time, I'm sure about this: Beginning the class method declaration with virtual in a base class (class A), makes the method virtual for all classes derived from the base class, including the derived ones of the derived classes. (B and C class methods determined to be virtual).

到目前为止,我确信这一点:在基类(类 A)中以 virtual 开头的类方法声明,使从基类派生的所有类的方法成为 virtual,包括派生类的派生类。(B 和 C 类方法确定为虚拟)。

The confusion is here. What if, in the base class A, there wouldn't be any virtual member. Instead, let's say, that class B declares a method to be virtual. I assume, that this change would make the function virtual for all the derived classes that belong to the inheriting chain (C and D classes). So logically, B for C and D, is a sort of their "base class", right? Or am I wrong?

混乱就在这里。如果在基类 A 中没有任何虚拟成员会怎样。相反,假设类 B 将方法声明为虚拟方法。我假设,此更改将使属于继承链(C 和 D 类)的所有派生类的函数成为虚拟函数。所以从逻辑上讲,B 代表 C 和 D,是一种他们的“基类”,对吧?还是我错了?

回答by kist

You're correct.

你是对的。

I think that in this case the best solution is to try:

我认为在这种情况下最好的解决方案是尝试:

#include <iostream>

using namespace std;

class A {
   public:
      void print(){ cout << "print A" << endl; };
};

class B: public A {
    public:
       virtual void print(){ cout << "print B" << endl; };
};

class C: public B {
     public:
        void print(){ cout << "print C" << endl; };
};

int main()
{
   A *a = new C();
   B *b = new C();

   a->print(); // will print 'print A'
   b->print(); // will print 'print C'

   return 1;
}

回答by theta

You are entirely correct. Child inherits what its ancestors have. Base classes can't inherit what the child has (such as a new function or variable). Virtual functions are simply functions that can be overridden by the child class if the that child class changes the implementation of the virtual function so that the base virtual function isn't called.

你是完全正确的。孩子继承了祖先所拥有的东西。基类不能继承子类所拥有的(例如新函数或变量)。虚函数只是可以被子类覆盖的函数,如果该子类更改了虚函数的实现以便不调用基虚函数。

A is the base class for B,C,D. B is a the base class for C, D. and C is the base class for D too.

A 是 B、​​C、D 的基类。B 是 C、D 的基类,C 也是 D 的基类。

回答by theta

Of course you can do it. Virtual method is optional to override so it doesn't matter that you declare it in class A or B. If you dont want to use that method in class A then simply declare in in class B.

你当然可以做到。虚拟方法是可选的,可以覆盖,因此在 A 类或 B 类中声明它无关紧要。如果不想在 A 类中使用该方法,则只需在 B 类中声明即可。