派生类中的虚拟限定词 [英] virtual qualifier in derived class
问题描述
虚拟限定符是否对基类的虚函数起作用,在派生类中有什么区别?
Does the virtual qualifier to a virtual function of base class, in the derived class makes any difference ?
class b
{
public:
virtual void foo(){}
};
class d : public b
{
public:
void foo(){ .... }
};
或
class d : public b
{
public:
virtual void foo(){ .... }
};
这两个声明是否有任何区别,除了它使d的孩子意识到虚拟性foo()吗?
Is there any difference in these two declarations, apart from that it makes child of d make aware of virtuality of foo() ?
推荐答案
这没什么区别。 foo
在从b(及其后代)派生的所有类中都是虚拟的。
It makes no difference. foo
is virtual in all classes that derive from b (and their descendants).
根据C ++ 03标准,第10.3.2节:
From C++03 standard, §10.3.2:
如果在Base类和派生类中声明了虚拟成员函数vf,则直接或间接派生从Base声明具有与Base :: vf相同名称和参数列表的成员函数vf,然后Derived :: vf也是虚拟的(是否这样声明),并且它将覆盖基数:: 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 and same parameter list as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf.
这篇关于派生类中的虚拟限定词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!