#include <iostream>
#include <typeinfo>
using namespace Std;
Class Base
{
Public
virtual void Funca ()
{
cout << "base" << Endl;
}
};
Class derived:p ublic base
{
Public
virtual void FUNCB ()
{
cout << "derived" << Endl;
}
};
void FUNCC (base* p)
{
Derived *DP = dynamic_cast<derived *> (p);
if (DP! = NULL)
{
DP->FUNCB ();
}
Else
{
P->funca ();
}
}
FUNCD with typeid operator
void Funcd (Base *p)
{
derived* DP = NULL;
if (typeid (*p) = = typeID (derived))
{
DP = static_cast<derived*> (p);
DP->FUNCB ();
}
Else
P->funca ();
}
int main ()
{
Base *CP = new derived;
cout << typeid (CP). Name () << Endl;
cout << typeid (*CP). Name () << Endl;
FUNCD (CP);
FUNCC (CP);
Base *DP = new Base;
FUNCC (DP);
FUNCD (DP);
System ("pause");
return 0;
}
The result of the operation is:
Class Base *
Class derived
Derived
Derived
Base
Base
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Introduction of additional overhead in C + +