getting an object's type

J

Jim Strathmeyer

I'm writing code with a whole lot of inheritance and am finding myself
getting an object's type a lot. I do this by having a enumerated data
member in the base class, and setting it in the derived classes'
constructors (and getting the type through a member function.)

Is there any way to do this automatically? I've check the normal places,
and can't find anything about this. (Please let me know if I've missed
something.)

Or, is this approach representative of bad programming style?
 
G

Greg Schmidt

I'm writing code with a whole lot of inheritance and am finding myself
getting an object's type a lot. I do this by having a enumerated data
member in the base class, and setting it in the derived classes'
constructors (and getting the type through a member function.)

Is there any way to do this automatically? I've check the normal places,
and can't find anything about this. (Please let me know if I've missed
something.)

Does dynamic_cast do anything like what you need?
Or, is this approach representative of bad programming style?

It sounds like it might be. Tell us more about the problem you're
trying to solve. Without the background information, it's hard to give
suggestions on better methods. For example, I suspect there might be a
way to get virtual functions to do everything you need, but then again
there might not be.
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

Jim Strathmeyer said:
I'm writing code with a whole lot of inheritance and am finding myself
getting an object's type a lot. I do this by having a enumerated data
member in the base class, and setting it in the derived classes'
constructors (and getting the type through a member function.)

Is there any way to do this automatically?

The automatic way of doing this is called polymorphism and can be achieved
in two ways:

1) At run time by using virtual functions

2) At complie time by using templates
I've check the normal places,
and can't find anything about this. (Please let me know if I've missed
something.)

Or, is this approach representative of bad programming style?

Virtual functions should work in general. You might also want to investigate
the 'Visitor Pattern' which worked quite well for me in the past.

Ali
 
P

Peter Koch Larsen

Jim Strathmeyer said:
I'm writing code with a whole lot of inheritance and am finding myself
getting an object's type a lot. I do this by having a enumerated data
member in the base class, and setting it in the derived classes'
constructors (and getting the type through a member function.)

Is there any way to do this automatically? I've check the normal places,
and can't find anything about this. (Please let me know if I've missed
something.) dynamic_cast?

Or, is this approach representative of bad programming style?
Probably.
 

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
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top