Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ question: dynamic_cast



thanks for the answer

On Jul 13, 2008, at 8:50 PM, Andrew Demkin wrote:

Why are you expecting a NULL value?

because there is a RTTI information that aa is an instance of the class B
(at least gdb is perfectly aware that aa is an instance of B)



The compiler can see that a dynamic_cast from A* to A* is meaningless and therefore do nothing.

you're probably right


If you can, it'd be good to get rid of the void* return types -- they're masking this error (and possibly others).

I'd be happy to do that, but code is pretty old and isn't mine
I cannot change it (theoretically I can, but I have to convince a lot of people to do that
but it is quite possible that I won't have any choice and I'll change that old code)

I just found very obscure bug in that code and I'm exploring the ways how to fix it 

thanks again



HTH,
Andrew


On Jul 13, 2008, at 4:37 PM, Dmitry Markman wrote:

Hi, all

I accidentally encountered the following code (not exactly of course that is very simplified version)

class V {
    virtual int getID() = 0;
};

class A : public V {
public:
    virtual int getID(){return 0;}
    virtual int getIDA(){return 10;}
};

class B  : public V {
public:
    virtual int getID(){return 1;}
    virtual int getIDB(){return 11;}
};

A *a = new A();
B *b = new B();


void *getA() {
    return a;
}

void *getB() {
    return b;
}

int main (int argc, char * const argv[]) {
    A *sa = static_cast<A *>(getB());
    A *aa = dynamic_cast<A *>(sa);
//here aa ISN'T NULL!!!!!

    return 0;
}


in that fragment 
aa isn't null
I'd expect NULL, of course
NOTE: gdb clearly shows that aa (or sa)  is an instance of the B class (with set print object on and set print vtbl on)
(gdb) p aa
$2 = (B *) 0x106940
(gdb) p *aa
$3 = (B) {
  <V> = {
    _vptr$V = 0x2078
  }, <No data fields>}
(gdb) 




from other hand
the following code behaves just fine

int main (int argc, char * const argv[]) {
    V *v = static_cast<V *>(getB());
    A *aa = dynamic_cast<A *>(v);
//here aa IS NULL which is good

    return 0;
}

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden

Dmitry Markman


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden

References: 
 >C++ question: dynamic_cast (From: Dmitry Markman <email@hidden>)
 >Re: C++ question: dynamic_cast (From: Andrew Demkin <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.