Calling virtual function of derived class from base class constructor?

前端 未结 4 1534
时光取名叫无心
时光取名叫无心 2020-12-11 01:24

I´m trying to accomplish the same which is described in a previous question:

virtual function call from base class

But, my real question is:

What if

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 01:31

    Even though it's a virtual function, the base's version will get called since the derived class isn't fully constructed yet. The base class constructor is called before the derived class constructor, so if the derived virtual function were to get called, it would be with an incompletely-initialized instance - a possible (probably) recipe for disaster.

提交回复
热议问题