On Jan 4, 6:59*pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
k...@blueskied.com wrote:
I've got a abstract class CBase and two classes CPoly1 and CPoly2 that
inherit from CBase.
Is there a way to find out the classtype (CPoly1 or CPoly2) when
iterating over a list of objects for example.
A list of what objects? *And why do you need to find out the type?
FWIW I have foundd it useful for debugging. For instance if a
dynamic_cast fails unexpectedly then it is useful to get the actual
class type that failed the cast from the exception message.
<...>
>
WHAT FOR? *If you have designed your list to be of polymorphic
objects, use polymorphism, FCOL.
And (yep) the way to do it is to create an interface to dump the info
maybe something like (untested) :
struct abc_type_reflecter{
~abc_type_reflector(){}
virtual std::string get_type() const =0;
};
template <typename Base, typename Derived>
struct type_reflecter : Base, abc_type_reflecter{
std::string get_type() const { return typeid(Derived).name();}
};
// derive from my_base_class via type_reflecter
struct my_type : type_reflecter<my_base_class_type,my_type>{
...
};
To get the info you will, of course, need to dynamic_cast from your
base_class_type to an abc_type_reflector first.
You could also of course add e.g virtual_base_type_reflector or other
varieties derived somehow from abc_type_reflector
regards
Andy Little