什么时候使用动态的可接受示例

这个问题在这里已经有了答案:

  • 什么时候应该使用static_cast,dynamic_cast,const_cast和reinterpret_cast? 6个答案

  • @ user997112

    [在底部编辑]

    你好。 下面我们使用具有共同祖先的随机多态指针集合
    通过通用接口。
    额外的工作是使用其中一个特定派生类完成的
    我们需要dynamic_cast或typeid来了解这个....
    主要功能有呼叫
    然后是类声明
    那么动态演员阵容就结束了
    没有显示用new创建的对象的删除

    #include <iostream>
    #include <algorithm>
    #include <random>
    #include <exception>
    using namespace std;
    
    int dynamic_test();
    int main()
    {
        cout << "Hello world!" << endl;
        dynamic_test();
        return 0;
    }
    

    ............

    class basex {
        public:
        virtual ~basex() {};
        virtual void work() const = 0;
    };
    class next1x : public basex {
        public:
        void work() const override {cout << "1";/*secret*/}
    };
    class next2x : public basex {
        public:
        void work() const override {cout << "2";/*secret*/}
    };
    class next3x : public basex {
        public:
        void work() const override {cout << "3";/*secret*/}
    };
    
    std::vector<basex *> secret_class_picker()
    {
        //pick classes with common base at random
        std::random_device rd;
        std::uniform_int_distribution<int>  ud(1,3);
        std::mt19937 mt(rd());
        std::vector<int> random_v;
        for (int i = 0; i < 22; ++i)
            random_v.push_back( ud(mt) );
        cout << "Random" << endl;
        for ( auto bq : random_v) //inspecting for human reader
            cout << bq << " ";
        std::vector<basex *> v;
        basex * bptr;
        for (auto bq : random_v) {
                switch(bq)
                {
    
                default: throw std::exception(); break;
                case 1: bptr = new next1x; break;
                case 2: bptr = new next2x; break;
                case 3: bptr = new next3x; break;
                }
    
                v.push_back(bptr);
        }
        cout << "Objects Created " << v.size() << endl;
        return v;
    
    }
    
    //this function demands a more derived class
    int special_work(const next3x *)
    {
        //elided
        cout <<"[!]";
        return 0;
    }
    
    int dynamic_test()
    {
        std::vector<basex *> v = secret_class_picker();//delete these pointer later
        cout <<"Working with random polymorphic pointers"<<endl;
        for (const auto bq : v)
        {
            bq->work();//polymorphic
            next3x * ptr = dynamic_cast<next3x *>(bq);
            if (nullptr != ptr) special_work(ptr); //reserved for particular type
        }
        return 0;
    }
    

    ......................另类

    int dynamic_static_typeid()
    {
        std::vector<basex *> v = secret_class_picker();
        cout <<"Working with random polymorphic pointers"<<endl;
        int k(0);
        for (const auto bq : v)
        {
            bool flipflop = (k % 2) == 0;
            bq->work();//polymorphic
            //cout << "[*]"<< typeid(*bq).name();//dereference
    
            if (flipflop) {
                next3x * dc_ptr = dynamic_cast<next3x *>(bq);//not constant time in general
                if (nullptr != dc_ptr) {
                    special_work(dc_ptr); //reserved for particular type
                    ++k;
                }
            }
            else {
                if (typeid(next3x) == typeid(*bq)){//constant time
                    auto sc_ptr = static_cast<next3x *>(bq);//constant time
                    special_work(sc_ptr);
                    ++k; cout <<"[sc]";
                }
            }
            cout << endl;
        }
        return 0;
    }
    
    链接地址: http://www.djcxy.com/p/28705.html

    上一篇: Acceptable example of when to use a dynamic

    下一篇: What is the benefit/use of dynamic