-3
class Base 
{
        public:
        virtual void print (){cout<<"Base"<<endl;}
};

class Derived :public Base
{
        public:
        virtual void print (){cout<<"Derived"<<endl;}
};

int main(){
        Base *ptrb1=new Base();
        //it will print Base
        ptrb1->print();

        Base *ptrb2=new Derived();
        // it will print Derived
        ptrb2->print();

        Derived *ptrd=NULL;
        //what is the difference between 2ways?
        //what is the benefit of useing dynamic_cast?
        ptrd=dynamic_cast<Derived*>(ptrb2);
        ptrd->print();
}

what is the benefit( or difference between) of dynamic_cast if we can make the base class see the members of derived class by adding virtual function and make the base Indicates to obj from derived class

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91

3 Answers3

4

If you were to not use a cast, then non-virtual member functions would be calling Base methods. The following code shows the difference between the virtual and non-virtual functions, and what happens after dynamic casting.

class Base
{
public:
    void print2() {
        cout << "Base" << endl;
    }
    virtual void print() {
        cout << "Base" << endl;
    }
};

class Derived : public Base
{
public:
    void print2() {
        cout << "Derived" << endl;
    }
    virtual void print() {
        cout << "Derived" << endl;
    }
};

int main()
{
    Base* ptrb1 = new Base();
    //it will print Base
    ptrb1->print();
    ptrb1->print2();

    Base* ptrb2 = new Derived();
    // it will print Derived
    ptrb2->print();
    ptrb2->print2();

    Derived* ptrd = NULL;
    //what is the difference between 2ways?
    //what is the benefit of useing dynamic_cast?
    ptrd = dynamic_cast<Derived*>( ptrb2 );
    ptrd->print();
    ptrd->print2();
}

If you were to use a static_cast (as you might be inclined to think you can do when re-casting pointers) then a different class, still derived from Base, would be cast incorrectly. dynamic_cast would show the cast to not be possible and return nullptr instead.

Matt
  • 7,100
  • 3
  • 28
  • 58
  • 1
    And therefore my answer is incorrect and should be downvoted. @LaszloPapp – Matt Apr 14 '14 at 22:15
  • You are encouraging people to ask the same question again and again because they will get answers. This is not worth the reputation because it hurts the quality of the site, but it is easy reputation for some people, so no wonder they jump on it. – László Papp Apr 14 '14 at 22:17
  • Doesn't answer the question anyway. The Q is about `dynamic_cast` vs. virtual functions, not about `dynamic_cast` vs. `static_cast`. (I didn't vote either way.) – aschepler Apr 14 '14 at 22:18
  • @aschepler, my question answered that in the first line. You want to cast because of non-virtual functions. You want to `dynamic_cast` because you don't want to `static_cast`. Added code. – Matt Apr 14 '14 at 22:23
  • "dynamic_cast would show the cast to not be possible and throw an error instead." - it only throws for casts to references, it returns nullptr for failed pointer casts. – Tony Delroy Apr 14 '14 at 23:18
  • @TonyD, yep I've corrected it now. – Matt Apr 14 '14 at 23:20
0

Use virtual functions like this when you can.

Use dynamic_cast if:

  • you can't figure out a way to do it with virtual functions, or
  • the function is so specific to the derived class it doesn't make sense to put anything about it in the base interface, or
  • you need to find out whether or not your base pointer really is a particular type of derived, instead of just asking it to do or get something.
aschepler
  • 70,891
  • 9
  • 107
  • 161
0

dynamic_cast provides a subclass check based on run-time type information, answering the question:

is the this base class object of a specific derived type?

if the answer is yes (the cast succeeds), then we can use the object as its derived type, otherwise we cannot.

Without run-time-type info and dynamic_cast, C++ programmers would have to cob together their own ad-hoc type tests to do the same thing.

Why you sometimes want an explicit type check is that some small action needs to be done that is specific to a subtype, and you don't want to bring in the cumbersome formalism of a virtual method just for that small thing.

Other casts such as static_cast differ from dynamic_cast in that they do not make a run-time type check, and do not fail: rather than fail, they can produce a bogus result when they force a conversion which doesn't make sense.

Kaz
  • 55,781
  • 9
  • 100
  • 149