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: Fred Zwarts
Subject: Re: dynamic_cast from base to another parent of derived class
Date: Thu, 5 Oct 2006 09:06:52 +0200

"Boris" <boris@gtemail.net> wrote in message 
4og3oiFe9ihpU1@individual.net">news:4og3oiFe9ihpU1@individual.net...
> [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 

Interesting. A few years ago I had a similar problem on an OpenVMS system.
The dynamic_cast did not work properly in a function that was called from
another function in a shareable image (which is similar to a dynamic library 
under Linux). An example was code running in a separate thread. Since the
pthread library was in a shareable image, functions running in another thread
were called from functions in a shareable image.
The OpenVMS engineers brought out a patch for the compiler and the run-time 
library quickly.

The similarity of these problems strikes me, as the compiler environments are
from very different teams. Apparently there is something in dynamic_cast
that makes it difficult to work from pre-linked parts of a program.

Fred.Zwarts.


reply via email to

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