This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: question


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]