C++构造器和析构器的运行顺序

#include 
#include 
class BaseClass{
    public:
        BaseClass();
        ~BaseClass();
        void doSomething();
};

class SubClass : public BaseClass{
    public:
    SubClass();
    ~SubClass();

};
BaseClass::BaseClass(){

    std::cout << "进入基类构造器\n" ;
    std::cout << "在基类构造器做了某事\n";
};

BaseClass::~BaseClass(){

    std::cout << "进入基类析构器\n" ;
    std::cout << "在基类析构器做了某事\n";
};
void BaseClass::doSomething(){

    std::cout <<"do something\n";
};


SubClass::SubClass(){

    std::cout << "进入子类构造器\n" ;
    std::cout << "在子类构造器做了某事\n";
};

SubClass::~SubClass(){

    std::cout << "进入子类析构器\n" ;
    std::cout << "在子类析构器做了某事\n";
};


int main(){
    SubClass subClass;
    subClass.doSomething();
    std::cout << "完工\n";
    return 0;

}

C++构造器和析构器的运行顺序_第1张图片

你可能感兴趣的:(软件工程)