What is the benefit/use of dynamic

This question already has an answer here:

  • What is the advantage of using dynamic_cast instead of conventional polymorphism? 5 answers
  • When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used? 6 answers

  • 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.


    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.

  • 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.

    链接地址: http://www.djcxy.com/p/28704.html

    上一篇: 什么时候使用动态的可接受示例

    下一篇: 动态的好处/用处是什么?