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]

Re: Turning off C++ casting?


Paolo Bonzini <paolo dot bonzini at polimi dot it> writes:

| s> But if I'm iterating over lots of
| > objects, I'd like to get rid off the cost of dynamic_cast when I know
| > for sure the objects will always be of this type.
| 
| assert and the pointer version of dynamic_cast are your friends if you don't 
| want #ifdefs.
| 
|   assert (dynamic_cast<ExtendedObject*>(&baseObj) != NULL);
|   ExtendedObject& obj = *reinterpret_cast<ExtendedObject*>(&baseObj);

Please write the last line as

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

-- Gaby


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