派生类的成员函数是否从基类继承虚拟? [英] Does derived class' member functions inherit virtualness from base class?
问题描述
假设我们有以下两个类,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 classBase
and in a classDerived
, derived directly or indirectly fromBase
, [...] thenDerived::vf
is also virtual (whether or not it is so declared).So yes.
这篇关于派生类的成员函数是否从基类继承虚拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!