This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Removing data member from virtual base causes wrong member function to be called
- From: Brian Dessent <brian at dessent dot net>
- To: Trent Apted <tapted at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Tue, 05 Jun 2007 00:24:21 -0700
- Subject: Re: Removing data member from virtual base causes wrong member function to be called
- References: <1245b3810706042341j256f2492ucb56b09060a55d92@mail.gmail.com>
- Reply-to: gcc-help at gcc dot gnu dot org
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