help-gplusplus
[Top][All Lists]
Advanced

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

Re: dynamic_cast from base to another parent of derived class


From: Maxim Yegorushkin
Subject: Re: dynamic_cast from base to another parent of derived class
Date: 4 Oct 2006 00:36:10 -0700
User-agent: G2/1.0

Boris wrote:
> [crossposted to gnu.g++.help]
>
> Boris wrote:
> > Boris wrote:
> >> [...] The code I talk about looks like this:
> >>
> >> if (typeid(b) == typeid(level2))
> >> {
> >>  const level1 *l1 = dynamic_cast<const level1*>(&b);
> >> }
> >
> > I changed the code for testing purposes and replaced dynamic_cast with
> > reinterpret_cast - this works.
> >
> > I changed then the code to make it look like this:
> >
> > if (typeid(b) == typeid(level2))
> > {
> >  const level2 *l2 = dynamic_cast<const level2*>(&b);
> >  const level1 *l1 = l2;
> > }
> >
> > This works, too. Is there any other possible explanation than a
> > compiler bug why a dynamic_cast<const level1*> should not work?

Why not just:

    if(level2 const* l2 = dynamic_cast<level2 const*>(&b))
    {
        // no need comparing typeinfo's
    }

?

>
> It looks like a problem with g++. The code I was talking about is in a
> shared library. When I link the executable statically dynamic_cast works.
> When I use however the shared library dynamic_cast returns 0. Same code but
> different behavior due to linking.
>
> There is a section "dynamic_cast, throw, typeid don't work with shared
> libraries" at http://gcc.gnu.org/faq.html#dso.

I wonder how you reached such a conclusion. Where on that page do they
say it does not work?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]