class Foo
{
private:
int _mem;
public:
Foo() : _mem(0)
{
CommonConstructor();
}
Foo(int mem) : _mem(mem)
{
CommonConstructor();
}
Foo(const Foo& f) : _mem(f._mem)
{
CommonConstructor();
}
// 我们所有的构造器都需要的代码段
void CommonConstructor()
{
printf("Constructing the object");
}
};
class Foo2
{
private:
int _mem;
public:
// 该构造器调用第二个称为基类构造器的构造器
Foo2() : Foo2(0)
{
}
// 下面这个构造器包含由所有构造器使用的公共代码
Foo2(int mem) : _mem(mem)
{
printf("Constructing the object");
}
Foo2(const Foo2& f) : Foo2(f._mem)
{
}
};
建站咨询热线
029-33273980