class base{
public:
base()
{
std::coutstd::endl;
std::cout"base constructor"std::endl;
func1();
std::coutstd::endl;
}
virtual ~base()
{
std::coutstd::endl;
std::cout"base distructor"std::endl;
func1();
std::coutstd::endl;
}
virtual void func1()
{
std::cout"base virtural func1"std::endl;
}
void func2()
{
std::cout"base member func2"std::endl;
func1();
std::coutstd::endl;
}
};
class derived:public base{
public:
derived()
{
std::coutstd::endl;
std::cout"derived constructor"std::endl;
func1();
std::coutstd::endl;
}
virtual ~derived()
{
std::coutstd::endl;
std::cout"derived distructor"std::endl;
func1();
std::coutstd::endl;
}
virtual void func1()
{
std::cout"derived virtual func1"std::endl;
}
};
int main()
{
base *point = new derived();
point-func2();
delete point;
return 0;
}
会有这样的输出
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
即使func1是虚函数,在base类和derived的构造函数和析构函数里面,都是调用自己类里面的func1。
而在普通成员函数func2调用func1,就会走虚函数的流程。