继承与virtual析构

// author :xserver

#include <iostream>
#include <string>
using namespace std;

class A
{
    int a;
public:
    A(int x):a(x){cout << "create A\t" << x << endl;}

    A(){    cout << "create A" << endl;}

//    ~A(){   cout << "delete A\t" << this->a << endl;}         //  子类无析构行动
    virtual ~A(){   cout << "delete A\t" << this->a << endl;}   //  子类的也会跟着析构

};

class B:public A
{
    int b;
public:
    B(int x):b(x){cout << "create B\t" << x << endl;}
    B(){    cout << "create B" << endl;}
    ~B(){   cout << "delete B\t" << this->b << endl;}
};



int main(int argc, char *argv[])
{
    A* a = new B(5);

    delete a;

    return 0;
}

你可能感兴趣的:(C++,c,C#)