By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

getting an object's type

P: n/a

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?
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Fri, 28 Jan 2005 15:58:43 -0500 (EST), Jim Strathmeyer wrote:
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.

--
Greg Schmidt gr***@trawna.com
Trawna Publications http://www.trawna.com/
Jul 23 '05 #2

P: n/a
Guess you're looking for RTTI. Try this:

http://www.two-sdg.demon.co.uk/curbr...pers/RTTI.html
Leslie

Jul 23 '05 #3

P: n/a
"Jim Strathmeyer" <no************@andrew.cmu.edu> wrote in message
news:Pi**********************************@unix44.a ndrew.cmu.edu...

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

Jul 23 '05 #4

P: n/a

"Jim Strathmeyer" <no************@andrew.cmu.edu> skrev i en meddelelse
news:Pi**********************************@unix44.a ndrew.cmu.edu...

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.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.