Find out class type of polymorphic object

K

karl

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.
Something like (CLASSTYPE(b) == CPoly2).
Thanks!


class CBase {
public:
virtual void func() = 0;
};

class CPoly1 : public CBase { };

class CPoly2 : public CBase { };

//method that goes through vector of polymorphic objects
CBase *b;
std::list<CBase*>::iterator it;
for (it=this->liste->begin(); !(it==this->liste->end()); it++) {
b=*it;
if (CLASSTYPE(b) == CPoly2) b->doSomethingSpecial(); // HOW TO
CHECK TYPE?
}
 
V

Victor Bazarov

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?
Something like (CLASSTYPE(b) == CPoly2).
Thanks!


class CBase {
public:
virtual void func() = 0;
};

class CPoly1 : public CBase { };

class CPoly2 : public CBase { };

//method that goes through vector of polymorphic objects
CBase *b;
std::list<CBase*>::iterator it;
for (it=this->liste->begin(); !(it==this->liste->end()); it++) {
b=*it;
if (CLASSTYPE(b) == CPoly2) b->doSomethingSpecial(); // HOW TO
CHECK TYPE?

WHAT FOR? If you have designed your list to be of polymorphic
objects, use polymorphism, FCOL.

V
 
K

karl

WHAT FOR?  If you have designed your list to be of polymorphic
objects, use polymorphism, FCOL.

For example i want to write a function that deletes all objects of
type CPoly2 in the list.
Of course i could add a property like "bool bIspoly2" in the CBase
class to mark objects of type CPoly2.
But is it a good way?
 
K

kwikius

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
 
V

Victor Bazarov

For example i want to write a function that deletes all objects of
type CPoly2 in the list.
Of course i could add a property like "bool bIspoly2" in the CBase
class to mark objects of type CPoly2.
But is it a good way?

Uh... No. Just delete them. If you follow the rules for making
polymorphic classes, your CBase has the virtual destructor. All
you need to do is 'delete list_element', and the proper destructor
is going to be called. What implementation for the "function that
deletes all objects" did you have in mind?

V
 
K

kwikius

Uh...  No.  Just delete them.  If you follow the rules for making
polymorphic classes, your CBase has the virtual destructor.  All
you need to do is 'delete list_element', and the proper destructor
is going to be called.  What implementation for the "function that
deletes all objects" did you have in mind?

FWIW the way I read it is he only wants to delete an object IF its an
X.

(Hint to O.P... look up 'dynamic_cast', which I assume is in the FAQ)

regards
Andy Little
 
K

karl

FWIW the way I read it is he only wants to delete an object IF its an

That's right. I only want to delete/process X objects.

Thanks for all the information, i will read it soon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top