| |||
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] |
| Why are you expecting a NULL value? The compiler can see that a dynamic_cast from A* to A* is meaningless and therefore do nothing. More important, however, your static_cast<A*>(getB()) results in undefined behavior because B* is not convertible to A*. Since getB() is returning void* rather than B*, the bad cast is going undetected. The compiler has to assume the static type information is accurate even though in this case it isn't (you've reinterpreted a B* as an A*). Note that the only safe form of static_cast on a void* is to the pointer type from which the void* originated. If you change getB() to return a B* and then try the following, dynamic_cast<A*>(getB()); or you leave the code unchanged and write this, dynamic_cast<A*>(static_cast<B*>(getB())); you'll get the NULL pointer you're expecting (because B* isn't convertible to A*). If you can, it'd be good to get rid of the void* return types -- they're masking this error (and possibly others). HTH, Andrew On Jul 13, 2008, at 4:37 PM, Dmitry Markman wrote:
|
_______________________________________________ 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>) |
| Home | Archives | FAQ | Terms/Conditions | Contact | RSS | Lists | About |
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.