A question about C++ classes

Started by
7 comments, last by MatsG 24 years ago
Hi! if I have a base class and then som derived classes, can I tell what derived class a class is by just having the base class pointer to it? Ex: class arne { ... } class sven:public arne { .. } class kalle:public arne { .. } void main(void ) { arne *clpoint; clpoint=new sven; if(clpoint is a sven)DoSomethingFun(); } Is there some keyword for the "clpoint is a sven"? Or do I have to make a function that returns what type it is?
Advertisement
Check out RTTI (Run Time Type Identification) in the manual for your C++ compiler. That will let you do exactly what you describe.

(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.

Yes, you can do this. Check in your C++ books for something called "Run-Time Type Information" (usually abbreviated as RTTI).

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!
That was exactly what I was looking for =)
thank you!
You can use dynamic_cast < sven * > (clpoint). It will either return a proper sven* object or...
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
"after many years of singularity, i'm still searching on the event horizon"
Ahhhh, Polymorphism...
Is there something it CAN'T Do!

Look what I learned today!!!





Edited by - Zipster on 4/7/00 3:10:31 PM
It cannont make your bed, wash your clothes, brush your teeth, go to school for you, the only thing it''s good for is making money for you
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! =)=)

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!

This topic is closed to new replies.

Advertisement