vector::push_back not work for non default constructor

后端 未结 4 1143
萌比男神i
萌比男神i 2021-01-28 13:26

i have a Controls class that have default constructor and copy constructor and other constructor ,and an assignment operator , and i want to create array of my cla

4条回答
  •  甜味超标
    2021-01-28 14:09

    This has nothing to do with std::vector and non-default constructors. You did not provide the whole code, but I assume this is the continuation of the previous question.

    Your Controls::operator= is invalid, it creates copies of QWidgets, and puts them into brand new QLayout. Controls object that you pass to push_back is temporary object that is destroyed after the call and it's copy is put into vector. But destroyed object's QWidget members are put into QLayout, which is not destroyed and which is added to the widget you try to show (Panel). After temporary Controls object is destroyed, Panel->show() calls Panel's QLayout methods that try to access already deleted widgets.

    Do you really need to save copies of your Controls objects in your vector? If you store pointers, that would get rid of your problem. Why do you need that vector at all?

    And once again, do not use auto_ptr, it's deprecated and you don't need it to correctly manage deletion of QObjects.

提交回复
热议问题