【发布时间】:2016-02-24 20:07:10
【问题描述】:
这是一个例子:
struct parent {
int a;
virtual void stuff() { a = 5; } //variant A
void roundabout() { stuff(); }
parent() { stuff(); }
};
struct child : parent {
void stuff() override { a = 6; } //variant B
child() : parent() {}
};
和用法
child c; //calls variant A
auto p = reinterpret_cast<parent*>(&c);
c.stuff(); //calls variant B
c.roundabout(); //calls variant B
p->stuff(); //calls variant B
p->roundabout() //calls variant B
所以在构造之后,无论我从课堂内部还是外部调用 stuff(),而无需明确说明 parent::stuff(),我都会得到 child::stuff(),正如预期的那样。
一个例外是仍然调用 parent::stuff() 的 parent 的构造函数,即使它是由子构造函数触发的。这真的很烦人,因为我必须在构造函数调用的函数中包含额外的逻辑,以使它们按应有的方式运行。为什么会这样?
【问题讨论】:
-
注意,同样的问题也适用于析构函数。父类构造函数不能调用子类中的虚方法,因为子类是在构造父类之后才构造的,而父类析构函数不能调用子类中的虚方法,因为子类在父类之前就已经被析构了被破坏了。