C++ vector push_back

前端 未结 7 948
旧时难觅i
旧时难觅i 2020-12-23 22:03

What is the proper way of pushing a new object element onto a std::vector? I want the data to be allocated in the vector. Will this copy the object newrad

7条回答
  •  长情又很酷
    2020-12-23 22:36

    Yup, radioNum will be copied into m_radios. As long as you're not deallocating pointers when newradio.~Radio(); occurs (out of scope), it's OK. If m_radios, or a subclass uses pointers, you'll need to make the switch to smart pointers (shared_ptr).

    When m_radios goes out of scope, the destructor is automatically called, and all the stuff std::vector holds is freed.

提交回复
热议问题