How to get the exact type of a pointer in run time for virtual functions?-Collection of common programming errors
You can do this by using dynamic_cast, or the typeid operator. However, generally this is a very bad idea. If your virtual function needs to know the exact type of the object, then something is wrong with your design.
If you have this situation:
class treeNode
{
virtual int foo()
{
// do some default thing, maybe nothing
}
};
class splitNode : public treeNode
{
virtual int foo()
{
// do whatever splitNode wants to do
}
};
class leafNode : public treeNode
{
virtual int foo()
{
// do whatever leafNode wants to do
}
};
and you have a pointer treeNode *p; that points to either a splitNode or a leafNode object, and you call p->foo(), then the appropriate version of foo() will be called. That is the whole point of virtual functions.
Also, see this question.