C++关于虚析构函数例子

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/houzijushi/article/details/81977518

C++关于虚析构函数例子

代码:

#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        cout << "Base()" << endl;
    }
    virtual ~Base() {
        cout << "~Base()" << endl;
    }
    virtual void func() {
        cout << "Base func()" << endl;
    }
};

class Derived : public Base {
public:
    Derived() {
        cout << "Derived()" << endl;
    }
    virtual ~Derived() {
        cout << "~Derived()" << endl;
    }

    virtual void func() {
        cout << "Derived func()" << endl;
    }
};


int main() {
    Base* pb = new Derived();
    pb->func();
    delete pb;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/houzijushi/article/details/81977518