Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 派生类析构函数发生了什么?_C++_Visual C++ - Fatal编程技术网

C++ 派生类析构函数发生了什么?

C++ 派生类析构函数发生了什么?,c++,visual-c++,C++,Visual C++,下面是我试图理解其输出的代码 class A { public: A(); ~A(){ cout<<"Destructor Called from A"<<endl;} A(int x):Z(x){ cout<<"Constructor Called from A"<<endl; }; private: int Z; }; class B:

下面是我试图理解其输出的代码

class A
    {
    public:

        A();
        ~A(){ cout<<"Destructor Called from A"<<endl;}
        A(int x):Z(x){ cout<<"Constructor Called from A"<<endl; };

    private:
        int Z;
    };

    class B:public A
    {
    public:

        B();
        ~B(){ cout<<"Destructor Called from B"<<endl;}
        B(int x):A(x),Y(x){ cout<<"Constructor Called from B"<<endl; };

    private:
        int Y;
    };

    int main() {

        A *a = new B(10);        
        delete a;

        return 0;
    }
我的问题是,类
B
的析构函数发生了什么?
对象切片在这里发挥作用了吗?

您需要使超类的析构函数成为虚拟的:

 class A {
    virtual ~A(){ cout<<"Destructor Called from A"<<endl;}
A类{

virtual~A(){cout您需要使超类的析构函数为虚拟的:

 class A {
    virtual ~A(){ cout<<"Destructor Called from A"<<endl;}
A类{
虚拟~A(){cout