我可以使用纯虚拟功能覆盖虚拟功能吗? [英] Can I override a virtual function with a pure virtual one?

查看:102
本文介绍了我可以使用纯虚拟功能覆盖虚拟功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个类别:BDG. DBGD. BD都是抽象的. B来自第三方.

I have three classes: B, D and G. D is a B and G is a D. Both B and D are abstract. B is from a third party.

B具有G需要实现的非纯虚拟方法(成为D).我可以并且将虚拟函数重新定义/覆盖为纯虚拟方法是一种好习惯吗?

B has a non-pure, virtual method that G needs to implement (to be a D). Can I and is it good practice to redefine/override a virtual function to be pure virtual?

示例:

class B // from a third party
{
public:
   virtual void foo();
};

class D : public B
{
public:
   void foo() override = 0; // allowed by gcc 4.8.2
   virtual void bar() = 0;
};

class G : public D
{
public:
   // forgot to reimplement foo
   void bar() override;
};

int main()
{
   G test;  // compiler error is desired
}

关于我可以吗?"的问题gcc允许这样做,但我没有术语/词汇表来验证该行为是标准的一部分还是未定义并在今天能正常工作.

To the question of "can I?" gcc allows it, but I do not have the terms/vocabulary to verify the behavior is part of the standard or is undefined and happens to work today.

推荐答案

您问过:

我可以使用纯虚拟函数覆盖虚拟函数吗?

Can I override a virtual function with a pure virtual one?

答案是:是的,可以.根据C ++ 11标准:

The answer is: Yes, you can. From the C++11 standard:

10.4抽象类

5 [注意:抽象类可以从非抽象的类派生,并且纯虚函数可能会覆盖非纯虚函数. — 尾注]

5 [ Note: An abstract class can be derived from a class that is not abstract, and a pure virtual function may override a virtual function which is not pure. —end note ]

这篇关于我可以使用纯虚拟功能覆盖虚拟功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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