派生类的成员函数是否从基类继承虚拟? [英] Does derived class' member functions inherit virtualness from base class?

查看:197
本文介绍了派生类的成员函数是否从基类继承虚拟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下两个类,A是具有虚拟析构函数的基类,B是其析构函数没有'virtual'限定符的派生类。我的问题是,如果我从B得到更多的类,将B的析构函数自动继承虚拟或我需要显式地将'virtual'之前'〜B(){... }'

  class A 
{
public:
A {std :: cout< create A<< std :: endl;};
virtual〜A(){std :: cout<< destroy A<< std :: endl;};
};

B类:A
{
public:
B(){std :: cout< create B<< std :: endl;};
〜B(){std :: cout<< 破坏B< std :: endl;};
};


解决方案

从C ++标准>


如果在 Base中声明了一个虚拟成员函数 vf ,直接或间接从中派生
a类派生 em> [...]
,然后 Derived :: vf 也是虚拟的(无论是否声明)。


是的。


Say we have the following two classes, A is the base class with virtual destructor and B is the derived class whose destructor doesn't have 'virtual' qualifier. My question is, if I going to derive more classes from B, will B's destructor automatically inherit the virtualness or I need to explicitly put 'virtual' before '~B() {...}'

class A
{
public:
    A() { std::cout << "create A" << std::endl;};
    virtual ~A() { std::cout << "destroy A" << std::endl;};
};

class B: A
{
public:
    B() { std::cout << "create B" << std::endl;};
    ~B() { std::cout << "destroy B" << std::endl;};
};

解决方案

From C++ standard (section 10.3):

If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, [...] then Derived::vf is also virtual (whether or not it is so declared).

So yes.

这篇关于派生类的成员函数是否从基类继承虚拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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