468,457 Members | 1,595 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Find out class type of polymorphic object

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?
}
Jan 4 '08 #1
7 5383
ka**@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?
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 4 '08 #2
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?
Jan 4 '08 #3
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
Jan 4 '08 #4
ka**@blueskied.com wrote:
>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?
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 4 '08 #5
On Jan 4, 8:07*pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
k...@blueskied.com wrote:
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?

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
Jan 4 '08 #6
ka**@blueskied.com wrote:
if (CLASSTYPE(b) == CPoly2) b->doSomethingSpecial(); // HOW TO
CHECK TYPE?
}
http://www.objectmentor.com/resources/articles/lsp.pdf
Jan 4 '08 #7
FWIW the way I read it is he only wants to delete an object IF its an
X.
That's right. I only want to delete/process X objects.

Thanks for all the information, i will read it soon.
Jan 4 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Martin Magnusson | last post: by
6 posts views Thread by Jon Slaughter | last post: by
9 posts views Thread by Brian Henry | last post: by
17 posts views Thread by Jef Driesen | last post: by
2 posts views Thread by Jessica | last post: by
7 posts views Thread by Charles Zhang | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.