c++ - 派生类的成员函数是否继承了基类的虚拟性?

标签 c++ inheritance virtual-functions

假设我们有以下两个类,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;};
};

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

最佳答案

来自 C++ 标准(第 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).

是的。

关于c++ - 派生类的成员函数是否继承了基类的虚拟性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6813624/

相关文章:

使用虚拟方法的 C++ 对象大小

c++ - 在 MSSQL 查询中使用 C++ 变量

c++ - 未定义、实现定义、未指定的行为都不是

c++ - 迭代合并排序,与冒泡排序速度相同

java - 使用泛型强制执行类声明

javascript - 在对象字面量中覆盖函数

c++ - 如何将 double vector 传递给构造函数,然后在子类中访问其数据(在 C++ 中)?

c++ - 我应该默认虚拟析构函数吗?

c++ - 将 const 基引用强制转换为派生类

c++ - 禁用某些方法的 Visual C++ 虚函数覆盖警告