Professional Documents
Culture Documents
=> runtime type identification : it allows you to identify the type of object du
ring the execution of your program .
=> casting operators give you safer, more controlled ways to cast .
[RTTI]
=> in non-polyphormic languages like c , there is no need for the runtime type
information because the type of each object is known at compile time .However in
polymorhic langauges such as c++, there can be situation in which the type of o
bject is unknown at compile time because the precise nature of that object is
not determined until the programs is executed .
#include<iostream>
#include<iomanip>
#include<typeinfo>
using namespace std;
virtual class Base
{
public:
int a;
};
virtual class Derived :public Base
{
public:
int b;
};
int main(void)
{
Base *ptr;
Derived d;
Derived *dPtr;
ptr = &d;
int i=33;
cout<<endl<<"the type of i is :"<<typeid(i).name();
cout<<endl<<"the type of ptr is :"<<typeid(ptr).name();
cout<<endl<<"the type of d is :"<<typeid(d).name();
cout<<endl;
if(typeid(dPtr) != typeid(ptr))
{
//
cout<<endl<<"not of same type";
}
else
{
cout<<endl<<"same type";
}
return 0;
}
_________________________________________
#include<iostream>
#include<iomanip>
#include<typeinfo>
using namespace std;
class Mammal
{
public :
virtual bool lays_eggs()
{
return false;
}
};
int main()
{
Mammal *p,AnyMammal;
Cat cat;
play play;
p = &AnyMammal;
cout<<endl<<"p is pointing to an object of type";
cout<<endl<<typeid(*p).name()<<endl;
p = &cat;
cout<<endl<<"p is pointing to an object of type";
cout<<endl<<typeid(*p).name()<<endl;
p = &play;
cout<<endl<<"p is pointing to an object of type";
cout<<endl<<typeid(*p).name()<<endl;
};
_________________________________________
[ dynamic_cast ]
=> the dynamic cast performs a runtime cast that verfies the validity of a ca
st . if the cast is invalid at the time dynamic_cast us executed , then the cast
fails.
the general form of cast is
dynamic_cast<target-type>(expr)
=> the dynamic_cast may be used to cast one type of pointer into another or typ
e of reference into another .
=> if the cast fails , then dynamic_cast evaluates to null if the cast involves
pointers. if the dynamic_cast on reference types failes , a bad_cast exception i
s thrown .
______________________________________
#include<iostream>
#include<iomanip>
#include<typeinfo>
using namespace std;
class Base
{
int a;
public:
virtual bool fun()
{
return false;
}
};
};
int main(void)
{
Base *bptr, b_obj;
Derived *dptr, d_obj;
bptr = &d_obj; // base pointer points to derived object
///////////////////////////////////////
bptr = &b_obj; // base pointer points to Base object
dptr = dynamic_cast<Derived *>(bptr); // error
if(!dptr)
{
cout<<endl<<"cast FAIL"; // this will execute
}
return 0;
}
______________________________________
#include<iostream>
#include<iomanip>
#include<typeinfo>
using namespace std;
class Base
{
int a;
public:
virtual void f()
{
cout<<endl<<"inside Base";
}
};
int main(void)
{
Base *bp,b_obj;
Derived *dp,d_obj;
/////////////////////////////////////
dp = dynamic_cast<Derived *>(&d_obj);
if(dp)
{
cout << "Cast from Derived * to Derived * OK.\n";
dp->f();
}
else
{
cout<<endl<<"Error";
}
cout<<endl;
/////////////////////////////////////
bp = dynamic_cast<Base *>(&d_obj);
if(bp)
{
cout << "Cast from Derived * to Base * OK.\n";
bp->f();
}
else
{
cout<<endl<<"Error";
}
cout<<endl;
////////////////////////////////////////////
bp = dynamic_cast<Base *>(&b_obj);
if(bp)
{
cout << "Cast from Base * to Base * OK.\n";
bp->f();
}
else
{
cout<<endl<<"Error";
}
cout<<endl;
////////////////////////////////////////////
dp = dynamic_cast<Base *>(&b_obj);
if(dp)
{
cout << "Cast from Base * to Derived * OK.\n";
dp->f();
}
else
{
cout<<endl<<"Cast from Base * to Derived * not OK. Error";
}
cout<<endl;
//////////////////////////////////////////////
bp = &d_obj;
dp = dynamic_cast<Derived *>(bp);
if(dp)
{
cout << "Casting bp to a Derived * OK\n" <<
"because bp is really pointing\n" <<
"to a Derived object.\n";
dp->f();
}
else
cout << "Error\n";
return 0;
}
_______________________________________________________________
[ const_cast ]
=> the const_cast is used to explicitly override const / or volatile in a cast.
the target type must be the same as the source type except for the alteration of
its const or volatile attibutes.
syntax :
const_cast<type>(expr)
#include<iostream>
#include<iomanip>
#include<typeinfo>
using namespace std;
void sqrVal(const int *pval);
int main()
{
int x=10;
cout<<endl<<"before:"<<x;
sqrVal(&x);
cout<<endl<<"after :"<<x;
return 0;
}
void sqrVal(const int *pval)
{
int *p;
p = const_cast<int *>(pval);
*p = *p * *p;
}
________________________________________________________
[ static_cast ]
=> the static_cast operator performs a nonpolymorhic cast.
The static_cast operator performs a nonpolymorphic cast. It can be used for any
standard conversion. No run-time checks are performed. Its general form is
static_cast<type> (expr)
// Use static_cast.
#include <iostream>
using namespace std;
int main()
{
int i;
for(i=0; i<10; i++)
cout << static_cast<double> (i) / 3 << " ";
return 0;
}
[reinterpret_cast]
=> the reinterpret_cast operator convertsone type into fundamentally different
type.
=> for example , it can change a pointer into an integer and an integer into a
pointer .
syntax :
reinterpret_cast<type> (expr)