【问题标题】:Why do constructors ignore overriden virtual functions? [duplicate]为什么构造函数忽略覆盖虚函数? [复制]
【发布时间】: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 的构造函数,即使它是由子构造函数触发的。这真的很烦人,因为我必须在构造函数调用的函数中包含额外的逻辑,以使它们按应有的方式运行。为什么会这样?

【问题讨论】:

  • 注意,同样的问题也适用于析构函数。父类构造函数不能调用子类中的虚方法,因为子类是在构造父类之后才构造的,而父类析构函数不能调用子类中的虚方法,因为子类在父类之前就已经被析构了被破坏了。

标签: c++ msvc12


【解决方案1】:

除非父实例已经存在,否则子实例不能存在。所以在 parent 的构造函数中,不能存在 child 的实例。那么如果没有孩子,你怎么能打电话给child::stuff呢?

【讨论】:

    猜你喜欢
    • 2017-07-11
    • 2020-04-11
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    相关资源
    最近更新 更多