c ++ virtual keyword vs overriding function

Sar*_*ski 8 c++ polymorphism virtual overriding

我正在学习c ++并且正在学习虚拟关键字.我已经在互联网上搜索,试图理解它无济于事.我进入我的编辑器并进行了以下实验,期望它打印出基本消息两次(因为我的印象是需要使用virtual关键字来覆盖函数).但是,它打印出两个不同的消息.有人可以向我解释为什么我们需要虚拟关键字,如果我们可以简单地覆盖函数并且看起来仍然看似多态行为?也许有人可以帮助我和其他人在未来理解虚拟与压倒一切.(我得到的输出是"我是基础",然后是"我是派生的").

#include <iostream>

using namespace std;
class Base{
public:
    void printMe(){
        cout << "I am the base" << endl;
    }
};
class Derived: public Base{
public:
    void printMe(){
        cout << "I am the derived" << endl;
    }
};
int main() {
    Base a;
    Derived b;
    a.printMe();
    b.printMe();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Cor*_*mer 12

请考虑以下示例.重要的一条线来说明需要virtualoverridec->printMe();.请注意,类型cBase*的,但是由于它的多态性是正确的能够从派生类中调用重写的方法.

#include <iostream>

class Base{
public:
    virtual void printMe(){
        std::cout << "I am the base" << std::endl;
    }
};

class Derived: public Base{
public:
    void printMe() override {
        std::cout << "I am the derived" << std::endl;
    }
};

int main() {
    Base a;
    Derived b;
    a.printMe();
    b.printMe();
    Base* c = &b;
    c->printMe();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是

I am the base
I am the derived
I am the derived
Run Code Online (Sandbox Code Playgroud)

  • 虽然,你还没有解释`override`关键字的用途是什么. (3认同)

Cur*_*ous 6

使用您拥有的代码,如果您这样做

Derived derived;
Base* base_ptr = &derived;
base_ptr->printMe();
Run Code Online (Sandbox Code Playgroud)

你觉得怎么样?它不会打印出来,I am the derived因为该方法不是虚拟的,并且调度是从调用对象的静态类型(即Base)完成的.如果将其更改为虚拟,则调用的方法将取决于对象的动态类型,而不是静态类型.


Mar*_*k R 5

override 是 C++11 中新增的关键字。

您应该使用它,因为:

  • 编译器将检查基类是否包含匹配virtual方法。这很重要,因为方法名称或其参数列表中的一些拼写错误(允许重载)可能会导致某些东西被覆盖而实际上并没有被覆盖的印象。

  • 如果你使用overridefor 一个方法,如果另一个方法被覆盖而不使用override关键字,编译器将报告错误。这有助于在发生符号冲突时检测不需要的覆盖。

  • virtual并不意味着“覆盖”。在类中不要使用“override”关键字而不是覆盖方法,您可以简单地编写此方法省略“virtual”关键字,覆盖将隐式发生。开发人员virtual在 C++11 之前编写以表明他们的覆盖意图。简单地说virtual就是:这个方法可以在子类中被覆盖。