Hi all, This is my first post. :-) Recently, I found an odd behavior about dynamic_cast across shared libraries.
This is my box:
linux kernel-2.4.21
gcc-3.4.3
(Check out my test_case.tar.bz2 for complete source
codes.)
I defined these classes and functions in libbase.so:
struct Base; // <- polymorphic
struct Tag; // <- non-polymorphic
struct VirtualTag; // <- polymorphic
Tag* getTag(Base* base)
{
return dynamic_cast<Tag*>(base);
}
VirtualTag* getVirtualTag(Base*)
{
return dynamic_cast<VirtualTag*>(base);
}
I also defined these derived classes in libderived.so:
struct Derived0 : Base, Tag;
struct Derived1 : Base, VirtualTag;
Then I tested getTag() and getVirtualTag() in two ways:
test0: linked to libderived.so (and libbase.so) at
build time.
test1: dynamically loads libbase.so and libderived.so
by using dlopen() at runtime.
These two test cases basically does the same thing:
1. create Derived(0|1) instance.
2. do dynamic_cast by using get[Virtual]Tag()
function.
In test0, both getTag() and getVirtual() are ok (returns
non-NULL value).
But in test1, getTag() returns NULL while getVirtualTag()
returns non-NULL.
I expected that I got the same results in both cases...
In test1, typeid(Tag) is not unique.
Could anyone tell which behavior is right or bug?
Thanks.
--------------------------------------
STOP HIV/AIDS.
Yahoo! JAPAN Redribbon Campaign 2005
http://pr.mail.yahoo.co.jp/redribbon/
test_cast.tar.bz2
Description: test_cast.tar.bz2
