Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

When in the parent class destructor, the v-table will point toward the parent's v-table not the child's (try it out), so I'm not sure how you'd call a virtual method accessing a child member.


Would it? Or is it undefined? No C++ runtime I know of will generate a new, different vtbl just to switch it out when invoking the non-owning base class destructor on an object. Nor am I familiar with any part of the C++ stnadrd that mandates this behaviour, or even mandates vtbls.


It doesn't create a new vtbl, when invoking the base object destructor it just repoints the current vtable ptr to the base object vtable as first thing:

   https://godbolt.org/z/MKx6oTE63
I can't recite chapter and verse, but I'm pretty sure that's mandated by the standard and it mirrors the behaviour in the constructor.

Or you meant something else?


Of course it would, not a lot would run otherwise. Here is very nice writeup about vtables and constructors: https://shaharmike.com/cpp/vtable-part4/


Section 11.9.5.4 of the C++23 standard describes the observable behaviour.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: