C ++“虚拟”派生类中的函数的关键字。有必要吗? [英] C++ "virtual" keyword for functions in derived classes. Is it necessary?

查看:93
本文介绍了C ++“虚拟”派生类中的函数的关键字。有必要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面给出的struct定义...

With the struct definition given below...

struct A {
    virtual void hello() = 0;
};

方法#1:

struct B : public A {
    virtual void hello() { ... }
};

方法#2:

struct B : public A {
    void hello() { ... }
};

这两种方法之间有没有什么区别来覆盖hello函数?

Is there any difference between these two ways to override the hello function?

推荐答案

它们是完全一样的。除了第一种方法需要更多的打字并且潜在地更清楚之外,它们之间没有区别。

They are exactly the same. There is no difference between them other than that the first approach requires more typing and is potentially clearer.

这篇关于C ++“虚拟”派生类中的函数的关键字。有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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