基类构造函数构造的空间再派生类实例中有用吗?
#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