Re: Removing data member from virtual base causes wrong member function to be called

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux