[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 06:28:23 -0700 |
User-agent: |
G2/1.0 |
Boris wrote:
> Maxim Yegorushkin wrote:
> > [...]
> >
> > Why not just:
> >
> > if(level2 const* l2 = dynamic_cast<level2 const*>(&b))
> > {
> > // no need comparing typeinfo's
> > }
> >
> > ?
>
> I want to cast to a level 1 class which is a parent of several level 2
> classes. Otherwise you are right - I could cast to the actual type directly.
> The real code looks more like this:
>
> if (typeid(b) == typeid(level2a) || typeid(b) == typeid(level2b) ||
> typeid(b) == typeid(level2c))
> {
> const level1 *l1 = dynamic_cast<const level1*>(&b);
> }
>
> Class level1 is the parent of all the level2 classes. I have to check for
> several types but don't need to write the same code then at least for all
> the level2 classes.
I would redesign my hierarchy so, that dynamic_cast would be enough.
> >> 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?
>
> The title is pretty clear I think: "dynamic_cast,throw, typeid don't work
> with shared libraries". :-) They are talking about template instantiations,
> vague linkage, not an exhaustive list, namesspaces etc. This all doesn't
> sound like adding a linker switch fixes all your problems. This sounds like
> be ready for all kind of problems.
This is a title of a frequently asked question. The answer is given fow
to make it work. If you can follow the first advise, which is
<q>
For a program which is linked against a shared library, no additional
precautions are needed
</q>
then no other action is required.
> Anyway if I can downcast to the actual type and then back to its parent with
> the following code (no compilation error, no runtime error) why should a
> dynamic_cast to the parent l1 not work?
>
> if (typeid(b) == typeid(level2))
> {
> const level2 *l2 = dynamic_cast<const level2*>(&b);
> const level1 *l1 = l2;
> }
You could find more help if you posted simplified complete code that
reproduces what you are observing.
It might be that you derive privately from your base (class A : B {};)
. dynamic_cast in this case does not work, reinterpret_cast does work
if your base class is first in the list of the base classes.
- Re: dynamic_cast from base to another parent of derived class, Boris, 2006/10/03
- Re: dynamic_cast from base to another parent of derived class, Maxim Yegorushkin, 2006/10/04
- Re: dynamic_cast from base to another parent of derived class, Boris, 2006/10/04
- Re: dynamic_cast from base to another parent of derived class, Boris, 2006/10/04
- Re: dynamic_cast from base to another parent of derived class,
Maxim Yegorushkin <=
- Re: dynamic_cast from base to another parent of derived class, Boris, 2006/10/04
- Re: dynamic_cast from base to another parent of derived class, Maxim Yegorushkin, 2006/10/05
- Re: dynamic_cast from base to another parent of derived class, Boris, 2006/10/05
- Re: dynamic_cast from base to another parent of derived class, Maxim Yegorushkin, 2006/10/05
- Re: dynamic_cast from base to another parent of derived class, F.J.K., 2006/10/10
- Re: dynamic_cast from base to another parent of derived class, Boris, 2006/10/10
Re: dynamic_cast from base to another parent of derived class, Fred Zwarts, 2006/10/05