Dynamic cast in C++

The dynamic_cast performs a run-time cast that verifies the validity of a cast. If the cast is invalid at the time dynamic_cast is executed, then the cast fails.
The general form of dynamic_cast is shown here:
dynamic_cast<target-type> (expr)
Here, target-type specifies the target type of the cast, and expr is the expression being cast into the new type. The target type must be a pointer or reference type, and the expression being cast must evaluate to a pointer or reference. Thus, dynamic_cast may be used to cast one type of pointer into another or one type of reference into another.
The purpose of dynamic_cast is to perform casts on polymorphic types.

For example, given two polymorphic classes B and D, with D derived from B, a dynamic_cast can always cast a D* pointer into a B* pointer. This is because a base pointer can always point to a derived object. But a dynamic_cast can also cast a B* pointer into a D* pointer only if the object being pointed to actually is a D object.
In general, dynamic_cast will succeed if the pointer (or reference) being cast is a pointer (or reference) to either an object of the target type or an object derived from the target type. Otherwise, the cast will fail. If the cast fails, then dynamic_cast evaluates to null if the cast involves pointers. If a dynamic_cast on reference types fails, a bad_cast exception is thrown.

Here is a simple example. Assume that Base is a polymorphic class and that Derived is derived from Base.
Base *bp, b_ob;
Derived *dp, d_ob;
bp = &d_ob;                // base pointer points to Derived object
dp = dynamic_cast<Derived *> (bp);   // cast to derived pointer OK
if(dp) cout << "Cast OK";
Here, the cast from the base pointer bp to the derived pointer dp works because bp is actually pointing to a Derived object. Thus, this fragment displays Cast OK. But in the next fragment, the cast fails because bp is pointing to a Base object and it is illegal to cast a base object into a derived object.
bp = &b_ob; // base pointer points to Base object
dp = dynamic_cast<Derived *> (bp); // error
if(!dp) cout << "Cast Fails";
Demonstration:
#include<iostream>
using namespace std;

struct B { virtual ~B() { } };
struct D : public B { };

int main() {

B *bptr, bobj;
D *dptr, dobj;

cout<<"base pointer points to derived object \n";

bptr = &dobj;//base pointer points to derived object

cout<<"addr of dobj="<<&dobj<<"\n";
cout<<"bptr="<<bptr<<"\n";

dptr = dynamic_cast<D *>(bptr);

if(dptr)
   { cout<<"Cast ok \n";
    cout<<"dptr="<<dptr<<"\n";
   }
else
    cout<<"Cast fails";

cout<<"\n";

cout<<"derived pointer points to base object \n";

bptr = &bobj;
dptr = dynamic_cast<D *>(bptr);

if(dptr)
    cout<<"Cast ok.";
else
    cout<<"Cast fails \n";
    cout<<"dptr="<<dptr<<"\n";
   
    getchar();
    return 0;
   
}
Output:
base pointer points to derived object
addr of dobj=0x22ff20
bptr=0x22ff20
Cast ok
dptr=0x22ff20

derived pointer points to base object
Cast fails
dptr=0

Source : Google

0 comments:

Post a Comment