基类构造函数构造的空间再派生类实例中有用吗?

#include <iostream>
using namespace std;

class Base
{
public:
    Base()
    {
        cout<<"Base contruction"<<endl;
        num = 1;

    }
    virtual ~Base()
    {
        cout<<"Base deconstruction"<<endl;
    }
    int num;
};

class Derived: public Base
{
public:
    Derived()
    {

        cout<<"Derived contruction "<<endl;
    }
    virtual ~Derived()
    {
        cout<<"Derived deconstruction"<<endl;
    }

};

int main()
{
    Derived derived;
    std::cout << derived.num;

}

输出:

Base contruction
Derived contruction
1Derived deconstruction
Base deconstruction

posted @ 2017-04-12 16:29  每天一点积累  阅读(186)  评论(0编辑  收藏  举报