c++ 构造函数调用virtual成员函数

2017-06-21  本文已影响0人  邓登登登

class Base {

public:

Base() { std::cout << "Base\n"; }

~Base() { std::cout << "~~Base\n"; }

public:

virtual void print() { std::cout << "Base print\n"; }

};

class Baby : public Base {

public:

Baby() { std::cout << "Baby\n"; print(); }

~Baby() { std::cout << "~~Baby\n"; }

public:

void print() override { std::cout << "Baby print\n"; }

};

int main()

{

Baby b;

return 0;

}

输出:

上一篇下一篇

猜你喜欢

热点阅读