虚拟功能可以被非虚拟功能覆盖吗? [英] Can a virtual function be overridden 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
和Derived2
之间是否有区别?
推荐答案
来自c ++ 11标准的 10.3虚拟函数部分(草稿n3337)第2点:
From section 10.3 Virtual functions of the c++11 standard (draft n3337) point 2:
如果在Base类和Derived类中声明了虚拟成员函数vf,则直接或间接派生 从Base开始,成员函数vf具有相同的名称,parameter-type-list(8.3.5),cv-qualification和refqualifier (或不存在)声明为Base :: vf,然后派生的:: vf也是虚拟的(无论是否 ),并且它会覆盖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屋!