虚拟函数可以被非虚函数覆盖吗? [英] Can a virtual function be overriden by a non-virtual function?
问题描述
在此程式码中:
class Base {
public:
virtual void method() = 0;
};
class Derived1 : public Base{
public:
virtual void method() override { }
};
class Derived2 : public Base{
public:
void method() override { }
};
Derived1
code> Derived2 ?
Is there any difference between Derived1
and Derived2
?
推荐答案
> c ++ 11标准(草案n3337)第2点:
From section 10.3 Virtual functions of the c++11 standard (draft n3337) point 2:
如果虚拟成员函数vf在类Base在类Derived中直接或间接派生
来自Base,具有相同名称的成员函数vf,参数类型列表(8.3.5),cv-qualification和refqualifier
(或缺少声明为Base :: vf,然后Derived :: vf也是virtual(无论它是否是
这样声明的),它覆盖Base :: vf。
If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list (8.3.5), cv-qualification, and refqualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf.
因此 Derived2 :: method
也是 virtual
,即使它没有明确声明为这样。
So Derived2::method
is also virtual
, even though it is not explicitly declared as such.
这篇关于虚拟函数可以被非虚函数覆盖吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!