This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

An odd behavior of dynamic_cast


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/

Attachment: test_cast.tar.bz2
Description: test_cast.tar.bz2


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]