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: Boris
Subject: Re: dynamic_cast from base to another parent of derived class
Date: Wed, 4 Oct 2006 00:41:36 +0300

[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?

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. From what I understand though 
there is not much you can do? I'm using version 3.4.6 of g++ which is the 
latest of the 3.4.x series.

The code construct as above with dynamic_cast is used in various project 
files. When building and linking the shared library some work, others don't. 
I don't know though on what it depends why some dynamic_casts don't work and 
falsely return 0. Is there anything I can do except replacing all 
dynamic_casts? Any g++ experts with ideas?

Boris 




reply via email to

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