Trying to assign vector of Base* from vector of Derived*

前端 未结 3 1949
谎友^
谎友^ 2020-12-11 01:45

This seems like a pretty basic problem, but I can\'t figure it out. I have a std::vector of raw pointers to Derived objects, and I just want to copy it to anot

3条回答
  •  没有蜡笔的小新
    2020-12-11 02:21

    push_back performs element-wise conversions. The assignment operator exists only between vectors of the same type.

    An easy solution is to use assign:

    v2.assign(v1.begin(), v1.end());
    

提交回复
热议问题