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: Removing data member from virtual base causes wrong member function to be called


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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]