首页 > 解决方案 > 为什么没有在clang中调用派生类析构函数

问题描述

为什么不使用 clang++ 10 调用派生的析构函数,它在 g++ 9 中按预期工作?为什么我会得到

BC 
DC 
BD 

代替

BC 
DC 
DD 
BD 

struct AA
{
    AA()
    {
        cout <<"BC"<<endl;
    }
    virtual ~AA()
    {
        cout <<"BD"<<endl;
    }    
};
struct AB: public AA
{
    AB()
    {
        cout <<"DC"<<endl;
    }
    ~AB() override
    {
        cout <<"DD"<<endl;
    }

};
    
int main()
{
    AA *x= new AB[1];
    delete [] x;
    return 0;
}

标签: c++virtualdestructor

解决方案


推荐阅读