Archived

This topic is now archived and is closed to further replies.

MatsG

A question about C++ classes

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites