Showing posts with label dynamic_cast. Show all posts
Showing posts with label dynamic_cast. Show all posts

Casting examples in C++ Linux


#include <QCoreApplication>
#include <iostream>
using namespace std;
class A
{
};
class B
{
};
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    cout<<"Hello"<<endl;

    A *aa = new A;

//    B *bb = static_cast<B*>(aa);//Invalid cast A* to B*
//    B *bb = const_cast<B*>(aa);//Invalid cast A* to B*
//    B *bb = dynamic_cast<B*>(aa);//Invalid cast A* to B* source is not polymorphic
      B *bb = reinterpret_cast<B*>(aa);//OK
    return a.exec();
}
//---------------------------
#include <QCoreApplication>
#include <iostream>
using namespace std;
class A
{
public:
    virtual ~A(){}//making source A as polymorphic
};
class B
{

};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    cout<<"Hello"<<endl;

    A *aa = new A;

//    B *bb = static_cast<B*>(aa);//Invalid cast A* to B*
//    B *bb = const_cast<B*>(aa);//Invalid cast A* to B*
      B *bb1 = dynamic_cast<B*>(aa);//Compiles ok but run time crash
      B *bb2 = reinterpret_cast<B*>(aa);//OK
    return a.exec();
}
//-----------------------------------------
#include <QCoreApplication>
#include <iostream>
using namespace std;
class A
{
};
class B:public A
{
};
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    cout<<"Hello"<<endl;
    A *aa = new A;
    B *bb1 = static_cast<B*>(aa);//Ok
    //B *bb2 = const_cast<B*>(aa);//Invalid cast A* to B*
    //B *bb3 = dynamic_cast<B*>(aa);//Invalid cast A* to B*
    B *bb4 = reinterpret_cast<B*>(aa);//OK
    return a.exec();
}

//-----------------------------------------
#include <QCoreApplication>
#include <iostream>
using namespace std;
class A
{
public:
    virtual ~A(){}
};
class B:public A
{
};
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    cout<<"Hello"<<endl;
    A *aa = new A;
    B *bb1 = static_cast<B*>(aa);//Ok
    //B *bb2 = const_cast<B*>(aa);//Invalid cast A* to B*
    B *bb3 = dynamic_cast<B*>(aa);//ok
    B *bb4 = reinterpret_cast<B*>(aa);//OK
    return a.exec();
}
//Dynamic Cast demos
Dynamic_cast is used for down casting  polymorphic base pointer to derived class poinetr to access member function defined by derived class in safe way.
#include <iostream>
#include <exception>
using namespace std;
class base { virtual void dummy() {} };
class derived: public base { int a; };
int main ()
 {
      try
            {
                base * pba = new derived;
                base * pbb = new base;
                derived * pd;
                pd = dynamic_cast<derived*>(pba);//d to b dynamic_cast ok
                              //because polymorphic base class elese run time error
                if (pd == 0)
                    cout << "Null pointer on first type-cast" << endl;
                pd = dynamic_cast<derived*>(pbb);
                if (pd == 0)
                    cout << "Null pointer on second type-cast" << endl;//gets printed
            }
            catch (exception& e)
            {
                cout << "Exception: " << e.what();
            }
            return 0;
        }
      
      #include <iostream>
        #include <typeinfo>
        using namespace std;
        int main ()
        {
            int * a;
            int b;
            a = 0; b = 0;
            if (typeid(a) != typeid(b))
            {
                cout << typeid(a).name()<<endl;//pi
                cout << typeid(b).name()<<endl;//i
            }
            return 0;
        }
Example Code:
        #include <iostream>
        #include <typeinfo>
        #include <exception>
        using namespace std;
        class base
        {
            virtual void f(){}
        };
        class derived : public base {};
        int main ()
        {
            try
            {
                base* a = new base;
                base* b = new derived;
                cout << typeid(*a).name() << '\n';//4base
                cout << typeid(*b).name()<<"\n";//7derived
            }
            catch (exception& e)
            {
                cout << "Exception: " << e.what() << endl;
            }
            return 0;
        }
Example Code:
        #include <typeinfo>
        #include <iostream>
        using namespace std;
        class A
        {
            public:
            virtual ~A();
        };
        int main()
        {
            A* a = NULL;
            try
            {
                cout << typeid(*a).name() << endl;
            }
            catch (bad_typeid)
            {
                cout << "Object is NULL" << endl;
            }
        }
Example Code:
        #include <iostream>
        using namespace std;
        struct A
        {
            virtual void f() 
            {
                cout << "Class A" << endl;
            }
        };
        struct B : A
        {
            virtual void f()
            {
                cout << "Class B" << endl;
            }
        };
        struct C : A
        {
            virtual void f()
            {
                cout << "Class C" << endl;
            }
        };
        void f(A* arg)
        {
            B* bp = dynamic_cast<B*>(arg);
            C* cp = dynamic_cast<C*>(arg);
            if (bp)
                bp -> f();
            else if (cp)
                cp -> f();
            else
                arg -> f(); 
        };
        int main()
        {
            A aobj;
            C cobj;
            A* ap = &cobj;
            A* ap2 = &aobj;
            f(ap);
            f(ap2);
        }
/*
class C
Class A
*/
Example Code:
#include<iostream>
using namespace std;
class Base
{
public:
virtual    void f(){cout<<"Base::f"<<endl;}
};
class Derive:public Base
{
public:
virtual     void f(){cout<<"Derived::f"<<endl;}
};
int main()
{
//CASE 1----------------------------------------------------------------
//Base *b = new Derive();
Base *pb;
Derive *pd;
//pb=pd;//No error
//pd=pb;error: invalid conversion from ‘Base*’ to ‘Derive*’

pb = dynamic_cast<Base*>(pd);
if(pd!=NULL)
{
cout<<"derive to base pass\n";
//pd->f();//run time crash
//pb->f();// run time rash
}
else if(pd==NULL)
{
cout<<"derive to base fail\n";
}
Base *b = new Derive();
Base *pb2;
Derive *pd2;
//pb2=pd2;//No error
//pd2=pb2;//error: invalid conversion from ‘Base*’ to ‘Derive*’

//CASE 2------------------------------------------------------------------
b = dynamic_cast<Base*>(pd2);
if(pd2!=NULL)
{
cout<<"derive to base pass2\n";
//pd2->f();//run time crash
//pb2->f();// run time rash
}
else if(pd2==NULL)
{
cout<<"derive to base fail2\n";
}
//CASE 3---------------------------------------------------------------
//Base *b = new Derive();
cout<<"31"<<endl;
Base *pb3;
cout<<"32"<<endl;
Derive *pd3;
cout<<"33"<<endl;
//pb=pd;//No error
//pd=pb;error: invalid conversion from ‘Base*’ to ‘Derive*’
/*
pd3 = dynamic_cast<Derive*>(pb3);//BOOOOM
cout<<"34"<<endl;
if(pb3!=NULL)
{
cout<<"base to derive pass 3\n";
}
else if(pb3==NULL)
{
cout<<"base to derive 3\n";
}
*/
//CASE 4-------------------------------------------------------------------
Base *b4 = new Derive();
//Base *pb4;
Derive *pd4;
pd4 = dynamic_cast<Derive*>(b4);//BOOM
cout<<"44"<<endl;
//if(pb4!=NULL)
//{
//cout<<"pb4 base to derive pass4\n";
//}
//else if(pb4==NULL)
//{
//cout<<"pb4 base to derive fail4\n";
//}

if(b4!=NULL)
{
cout<<"b4 base to derive pass4\n";
b4->f();//Derive::f
}
else if(b4==NULL)
{
cout<<"b4 base to derive fail4\n";
}
if(pd4!=NULL)
{
cout<<"pd4 base to derive pass4\n";
pd4->f();//Derive::f()
}
else if(pd4==NULL)
{
cout<<"pd4 base to derive fail4\n";
}
return 0;
}
Example Code:
#include<iostream>
using namespace std;
class Base
{
public:
    virtual void f(){cout<<"Base::f"<<endl;}
};
class Derive:public Base
{
public:
    virtual void f(){cout<<"iDerived::f"<<endl;}
};
int main()
{
Base *b = new Derived;
return 0;
}

        #include <iostream>
        using namespace std;
        struct A
        {
            virtual void f() 
            {
                cout << "Class A" << endl;
            }
        };
        struct B : A
        {
            virtual void f()
            {
                cout << "Class B" << endl;
            }
        };
        struct C : A
        {
            virtual void f()
            {
                cout << "Class C" << endl;
            }
        };
        void f(A* arg)//cobj the aobj
        {
            B* bp = dynamic_cast<B*>(arg);//cobj to bobj fails //arg2. aobj to bobj fails
            C* cp = dynamic_cast<C*>(arg);//cobj to cobj pass //arg2.  aobj to cobj fails

            if (bp)
        {
        cout<<"bp\n";
                bp -> f();
        }
            else if (cp)
        {
        cout<<"cp\n";
                cp -> f();
        }
            else
        {
        cout<<"else\n";
                arg -> f(); 
        }
        };
        int main()
        {
            A aobj;
            C cobj;
            A* ap = &cobj;
            f(ap); //Cp C

            A* ap2 = &aobj;
            f(ap2);//else A

        }