文章内容请参加:http://www.cppblog.com/izualzhy/archive/2012/09/17/190998.html。
以下是我自己写的一个测试用的例子:
test.h
#ifndef __test_h__
#define __test_h__
#include <iostream>
class Base
{
public:
Base() { init(); }
protected:
virtual void init() { std::cout << "base::init()" << std::endl; }
};
class Inherit : public Base
{
public:
Inherit() : Base() {}
protected:
void init() { std::cout << "Inherit::init()" << std::endl; }
};
#endif // __test_h__
main.cpp
#include "test.h"
int main( void )
{
Inherit i;
return 0;
}
程序运行结果:

本文介绍了一个 C++ 测试用例,展示了基类与派生类构造函数中虚函数的调用过程。通过具体代码实现,演示了如何在派生类构造时覆盖基类的虚函数并进行初始化。
1108

被折叠的 条评论
为什么被折叠?



