The Other Worlds Shrine

Your place for discussion about RPGs, gaming, music, movies, anime, computers, sports, and any other stuff we care to talk about... 

  • Abstract programming question

  • Somehow, we still tolerate each other. Eventually this will be the only forum left.
Somehow, we still tolerate each other. Eventually this will be the only forum left.
 #89432  by Nev
 Mon Jun 27, 2005 5:11 am
To anyone and everyone (but probably mostly Kup):

Is there a datatype/operator in C++ that stores/returns *what* datatype another variable is - like System.Type and the typeof operator in C#?

I was looking, but I haven't seemed to find one, which sucks.

 #89433  by Nev
 Mon Jun 27, 2005 6:12 am
Nevermind! Answered my own question. <typeinfo> standard header file and the typeid operator, and joy for me in that they are even supported for the ARM compiler! woohoo!

I had another ridiculously long post as well asking a question, which answer I find out to be "declare the method virtual". It is AWESOME to actually be understanding inheritance as it's meant to be used.

 #89434  by Lox
 Mon Jun 27, 2005 8:48 am
Mental wrote:It is AWESOME to actually be understanding inheritance as it's meant to be used.
Congratulations. :)

 #89435  by Kupek
 Mon Jun 27, 2005 9:15 am
Be careful of <TT>typeid</TT> for two reasons. First, it's RTTI (Run Time Typing Information) which may be too expensive for your needs. Then again, it might be just fine. Make sure you measure to be sure.

Second, if you find yourself writing code like
Code: Select all
if (typeid(var) == typeid(A)) {
     // do something for object of type A
}
else if (typeid(var) == typeid(B)) {
     // do something for object of type B
}
else if (typeid(var) == typeid(C)) {
     // do something for object of type C
}
...
Then you should use virtual functions, not RTTI. The principle I go by is that RTTI should be a last resort; designs that avoid it are generally cleaner. Of course, there are some instances where it's the right thing to do.

 #89445  by Nev
 Mon Jun 27, 2005 1:53 pm
i have heard rtti can be expensive and for mobile phone development i believe i will need to carefully profile it. however, there's a generic "interactor" class that i believe would speed up development time for some future games i want to write, that i can't implement without it.

i will probably leave it out for the mobile development i'm doing, but i have to believe that on a more modern processor (say, a new AMD 3000+ or modern game console) the speed hit of rtti may easily be insignificant compared to the demands of graphics processing. i imagine it would depend on what i was doing - would i would want to stay away from doing, for example, extremely high numbers rtti calls per tick on a machine that wasn't ballsout blazing?

 #89449  by Kupek
 Mon Jun 27, 2005 2:31 pm
If you're interested in C++ in particular and object-oriented design in general - and it sounds like you are - I again reccomend buying and reading <a href="http://www.amazon.com/exec/obidos/tg/de ... books">The C++ Programming Language</a> by Bjarne Stroustrup (<a href="http://www.research.att.com/~bs/3rd.html">his page for it</a>). While he explains the language constructs, he also demonstrates the "correct" way to use them and talks about good design approaches. It's an excellent book and I think you'd get a lot out of it.

 #89452  by Nev
 Mon Jun 27, 2005 3:16 pm
I think so too. I've been broke, but I may have enough cash to pick it up now. Thanks for the help and, especially, the desire to help me improve, though!

 #89480  by Ishamael
 Tue Jun 28, 2005 12:29 am
...what Kupek said. And to add to that, you should only be writing code like Kupek showed generally in things like containers that can hold a mixed bag of unrelated objects.

 #89481  by Nev
 Tue Jun 28, 2005 12:34 am
Disagree. I have an idea for a general game framework that would benefit greatly from the use of RTTI. However, I don't want to get into a whole debate about it tonight so I'm going to pass on the description.