Re: Problem with static downcast of base type to derived type
On Jul 2, 10:48 pm, Jonathan Mcdougall <jonathanmcdoug...@gmail.com>
wrote:
On Jul 2, 4:31 pm, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
You can always cast from pointer to derived to pointer to
base using static_cast, but when going the other way you
shall use dynamic_cast.
"Shall" might be a bit strong here. There are cases where,
because of the context, downcasting cannot fail. In this case,
using dynamic cast gives you nothing except less performance.
It also gives you defined behavior in case you are mistaken.
You should use dynamic_cast until the profiler says otherwise,
even if you think you know that static_cast would be safe.
You could however have a separate function that uses dynamic
cast in debug but reverts to static cast in release,
You don't really mean to say that you deliver different code
than what you've tested, do you? Again, unless the profiler
says it's absolutely imperative, you deliver the same code,
compiled with the same options, as you use during development.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34