On 03/02/14 18:37, Graziano Servizi wrote:
How could this (included) very short code work?
It isn't a NOT SAFE conversion that made inside void fun?
How can the A pointer know about the B's member f() ?
I'm using gcc 4.8.2 on a Linux Fedora 19 system.
Thanks for your help.
G. Servizi
The a pointer knows nothing about B's member f().
The compiler sees that you are calling B::f(), and calls it (expecting a
B object) with a A object. As B::f() doesn't use anything of B, you are
“lucky” and it happens to run fine. If you were to call a B variable, it
would be accessing random meemory.
Equally, if it had to access the vtable for accesing f(), it would fail,
too. you can mark f() as “virtual” in order to force that and get at
runtime the Segmentation fault you seem to be expecting.
That this invalid code happens to «work» on this compiler version
doesn't make it more legal nor should it be taken as promoting its
(mis)use, etc. etc.