zoukankan      html  css  js  c++  java
  • virtual destructor

    code 1. 

    #include <iostream>
    using namespace std;

    class Base {
    public:
    Base() {cout<<"Base constructor"<<endl;}
    ~Base() {cout<<"Base destructor"<<endl;}
    };

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

    int main()
    {
    Derived *p = new Derived;
    delete p;

    return 0;
    }

    ---------------------

    [tli]$ ./a.out
    Base constructor
    Derived constructor
    Derived destructor
    Base destructor

    code 2.

    #include <iostream>
    using namespace std;

    class Base {
    public:
    Base() {cout<<"Base constructor"<<endl;}
    ~Base() {cout<<"Base destructor"<<endl;}
    };

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

    int main()
    {
    Base *p = new Derived;
    delete p;

    return 0;
    }

    -----

    [tli]$ ./a.out
    Base constructor
    Derived constructor
    Base destructor

    code 3.

    #include <iostream>
    using namespace std;

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

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

    int main()
    {
    Base *p = new Derived;
    delete p;

    return 0;
    }

    [tli]$ ./a.out
    Base constructor
    Derived constructor
    Derived destructor
    Base destructor

  • 相关阅读:
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    解决Jenkins生成测试报告的问题
  • 原文地址:https://www.cnblogs.com/Torstan/p/2734657.html
Copyright © 2011-2022 走看看