问题描述
我有3个类别:B
,D
和G
. D
是B
,G
是D
. B
和D
都是抽象的. 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.
推荐答案
您问过:
答案是:是的,可以.根据C ++ 11标准:
The answer is: Yes, you can. From the C++11 standard:
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 ]
这篇关于我可以使用纯虚拟功能覆盖虚拟功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!