Page 1 of 1
Abstract programming question
PostPosted:Mon Jun 27, 2005 5:11 am
by Nev
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.
PostPosted:Mon Jun 27, 2005 6:12 am
by Nev
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.
PostPosted:Mon Jun 27, 2005 8:48 am
by Lox
Mental wrote:It is AWESOME to actually be understanding inheritance as it's meant to be used.
Congratulations.
PostPosted:Mon Jun 27, 2005 9:15 am
by Kupek
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 allif (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.
PostPosted:Mon Jun 27, 2005 1:53 pm
by Nev
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?
PostPosted:Mon Jun 27, 2005 2:31 pm
by Kupek
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.
PostPosted:Mon Jun 27, 2005 3:16 pm
by Nev
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!
PostPosted:Tue Jun 28, 2005 12:29 am
by Ishamael
...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.
PostPosted:Tue Jun 28, 2005 12:34 am
by Nev
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.