Example to use shared_ptr?

后端 未结 7 1323
孤城傲影
孤城傲影 2020-12-02 05:08

Hi I asked a question today about How to insert different types of objects in the same vector array and my code in that question was

 gate* G[1000];
G[0] =         


        
7条回答
  •  一生所求
    2020-12-02 05:43

    The best way to add different objects into same container is to use make_shared, vector, and range based loop and you will have a nice, clean and "readable" code!

    typedef std::shared_ptr Ptr   
    vector myConatiner; 
    auto andGate = std::make_shared();
    myConatiner.push_back(andGate );
    auto orGate= std::make_shared();
    myConatiner.push_back(orGate);
    
    for (auto& element : myConatiner)
        element->run();
    

提交回复
热议问题