最近看到一个奇怪的问题,虽然我知道答案,测试结果也和我想象的一样,但就是不知道为什么?
结果输出如下:
----------------------------------------------
Object constructor: 0xbfffd9a0
this is Object f() print
Fruit constructor: 0xbfffd9a0
this is Fruit f() print
Apple constructor: 0xbfffd9a0
this is Apple print:
==============================================
分析:
按理,在构造Apple的时候,会挨个构造他所继承的基类,由于基类中会调用相关的函数, 从变量空间角度看,他应该调用的是Object::f()和Fruit::f(),比较奇怪的是,基类和派生类的this指针都是一样的,这样的话,为什么不是调用派生类的Apple::f()呢?
和this指针有关系么?
相关参考资料:
温昱 的构造函数中的this指针