This code has undefined behavior.
Note that inside the constructor, you can also call the virtual member function.
A somewhat complicated part is the invocation of a virtual member function during element initialization before the constructor code begins. It’s still valid, but it’s not obvious what is happening (the fact is that until the constructor code starts the object, the object is still not considered an instance of its class, and the virtual member functions are sent to the base class).
Some compilers give a warning if you use this in a member's initialization list precisely because the this pointer will behave strangely at this point (it will start to behave normally only after the constructor starts).
The code obviously works because most compilers use the VMT approach to dispatch methods, but VMT is not required to invoke a non-virtual method, and thus, if the method code does not play this out in any way, then it seems to work. However, the fact that the code seems to work in the implementation (or even in any implementation, for that matter) still does not make it legal C ++ code.
source share