An odd behavior of dynamic_cast
Shin-ichi MORITA
shin1_morita@yahoo.co.jp
Tue Dec 20 14:50:00 GMT 2005
Hi all,
This is my first post. :-)
# I could not find a mailing list dedicated to c++ at
gcc.gnu.org.
# So I post this mailing list.
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/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_cast.tar.bz2
Type: application/bzip2
Size: 1133 bytes
Desc: test_cast.tar.bz2
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20051220/ad950cb7/attachment.bin>
More information about the Gcc
mailing list