A question about C++ classes
(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.
HOWEVER, it''s generally not something you want to do. What is usually more appropriate is to make use of the polymorphism you are creating in your hierarchy by implementing virtual functions. This would look something like this:
class ame{ virtual void do_something_fun(void) { cout << "nothing" << endl; }};class sven : public ame{ void do_something_fun(void) { cout << "i am a sven" << endl; }};class kalle : public ame{ void do_something_fun(void) { cout << "i am a kalle" << endl; }};void main(void){ ame* clpoint; clpoint = new sven; clpoint->do_something_fun();}
When you run your program, your output will be:
i am a sven
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
dynamic_cast will return a NULL if clpoint is not a base class for sven. However, before that you need to enable RTTI (run-time type identification) in your compiler options.
Hmm... why not use virtual functions?
Edited by - DerekSaw on 4/6/00 10:23:46 PM
Is there something it CAN'T Do!
Look what I learned today!!!
Edited by - Zipster on 4/7/00 3:10:31 PM
Edited by - MatsG on 4/8/00 5:51:32 PM
quote: Original post by MatsG
Why not virtual functions... well I could use that but it´s for a undo function so I have a LOT of these classes and I´m to lazy to have another function in them =) and now I have learned a new thing! =)=)
Typically, RTTI will not save you anything over virtual functions. If you''re using RTTI, that means you''re writing a sequence of "if...else" statements to handle the code for each class.
Much easier to put the class-specific code in the class as a virutal function, make one call to that function, and not have to deal with RTTI.
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!