A question about C++ classes
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?
Check out RTTI (Run Time Type Identification) in the manual for your C++ compiler. That will let you do exactly what you describe.
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:
When you run your program, your output will be:
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
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!
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
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
Ahhhh, Polymorphism...
Is there something it CAN'T Do!
Edited by - Zipster on 4/7/00 3:10:31 PM
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
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
Popular Topics
Advertisement