Re: inheritance headache....
On Feb 1, 5:03 am, Sean Hunt <ride...@gmail.com> wrote:
[...]
// semi pseudo with
abstractTarget* createTarget(abstractDescription* desc)
{
// shortcut pseudo code for dynamic cast< > ()
if ( desc is concreteDesc1)
Don't use dynamic_cast to check whether a class is of a
specific type. Only use it if you need the actual dervied
object - otherwise, use typeid:
if (typeid(desc) == typeid(concreteDesc1))
typeid will return an std::type_info object corresponding to
the most derived type. Remember to include <typeinfo> before
using typeid!
The semantics are different. Use typeid if you want to know the
exact most derived type, dynamic_cast if you only want to know
whether the object is a, or not. (In this case, I suspect that
the desired semantics are in fact those of typeid, and not
dynamic_cast.)
[...]
As far as I know, there's no way to properly reproduce the virtual
call mechanism. However, a map of typeids will work:
bool operator <
Two gotcha's for mapping type_info's to anything:
1. A type_info is not copiable, so you can't put them in a
container. You need to use type_info const*.
2. type_info, per se, doesn't support operator<. You need to
provide your own, based on type_info::before(). (Hopefully,
the next version of the standard will correct this, and also
provide functions for == and getting a hash code of a
type_info.)
For these reasons, I tend to use a wrapper class, something
like:
class Type
{
public:
/* implicit */ Type( std::type_info const& type )
: myType( &type )
{
}
bool operator<( Type const& other ) const
{
return myType->before( *other.myType ) ;
}
private:
std::type_info const*myType ;
} ;
typedef std::map< Type, whatever >
--
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
TypeMap ;