473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 5634
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Martin Magnusson | last post by:
Hi! I have a class with a private member which is a pointer to an abstract class, which looks something like this: class Agent { public: void Step( Base* newB ); private:
6
by: Jon Slaughter | last post by:
Is there any way to get the class type from a pointer? I'm working on a class that acts as a set: I have two classes, one called FSet and the other called Element. FSet inherets Element and...
5
by: verec | last post by:
I just do not understand this error. Am I misusing dynamic_cast ? What I want to do is to have a single template construct (with no optional argument) so that it works for whatever T I want to...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
9
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over...
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
3
by: Simon Hart | last post by:
Hi, I am trying to implement some functionality as seen in MS CRM 3.0 whereby a basic Xml is deserialized into an object which contains properties. What I want to do from here is; cast the basic...
7
by: Charles Zhang | last post by:
I want to know to the following in a safe manner (the following code will be dangerous if p does not contain a pointer to MyClass). I would like someone to show me a better way (either throw an...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.