派生类中的虚拟限定词 [英] virtual qualifier in derived class

查看:83
本文介绍了派生类中的虚拟限定词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虚拟限定符是否对基类的虚函数起作用,在派生类中有什么区别?

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屋!

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