Turning off C++ casting?

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


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

| On Thursday 20 February 2003 13:17, Gabriel Dos Reis wrote:
| > Paolo Bonzini <paolo.bonzini@polimi.it> writes:
| > |  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<>.
| 
| You do because of the assert.

That is funny because you previously stated that you wanted to avoid
the dyanmic_cast<>.  Now, you're telling me that you are going to do it
anyway.  That is an *inconsistent* position.

|  So the correct answer to the poster's question 
| is to use
| 
| assert (dynamic_cast<ExtendedObject*>(&baseObj) != NULL);
| ExtendedObject& obj = *static_cast<ExtendedObject*>(&baseObj);

If you're going to do the dynamic_cast<>, then reuse the result:

  ExtendedObject* ptr = dynamic_cast<ExtendedObject*>(&baseObj);

  assert (ptr != 0);

  // use ptr

All this has nothing to do with GCC.

-- Gaby



More information about the Gcc mailing list