Removing data member from virtual base causes wrong member function to be called
Brian Dessent
brian@dessent.net
Tue Jun 5 07:24:00 GMT 2007
Trent Apted wrote:
> static void* make_image() {
> return new Image();
> }
>
> int main() {
> static_cast<Event*>(make_image())->trigger();
> return 0;
> }
>
> [...]
>
> Can anyone tell me what's going on?
I think you're invoking undefined behavior because you can't use
static_cast to downcast a pointer like that in the face of virtual
inheritance. You have to use dynamic_cast. I don't really understand
why you're intentionally obscuring things with the void* business, but
if you change it to "dynamic_cast<Event*>((Image
*)make_image())->trigger()" you get the desired behavior. The fact that
the undefined behavior just happens to depend on the layout of Resource
is incidental.
Brian
More information about the Gcc-help
mailing list