Turning off C++ casting?

Gabriel Dos Reis gdr@integrable-solutions.net
Thu Feb 20 12:28:00 GMT 2003


Paolo Bonzini <paolo.bonzini@polimi.it> writes:

| > |   assert (dynamic_cast<ExtendedObject*>(&baseObj) != NULL);
| > |   ExtendedObject& obj = *reinterpret_cast<ExtendedObject*>(&baseObj);
| >
| > Please write the last line as
| >
| >    ExtendedObject& obj = *dynamic_cast<ExtendedObject*>(&baseObj);
| 
| Why?

Because you don't know what reinterpret_cast<> will do.  Do you?

|  The point was to avoid the performance impact (assuming there is) of 
| dynamic_cast in non-debugging releases.

If you know for sure that the derivation actually holds, then use
static_cast<>, -not- reinterpret_cast<>.  


|  After the dynamic_cast has been 
| checked to be valid, you can use the faster reinterpret_cast safely, can't 
| you?

Maybe it is faster (as usual with wrong answers); but safely certainly not.

-- Gaby



More information about the Gcc mailing list