How to push_back without operator=() for const members?

前端 未结 1 794
滥情空心
滥情空心 2020-12-07 04:02

How to push_back() to a C++ std::vector without using operator=() for which the default definition violates having const members?

struct Item {
  Item(int va         


        
相关标签:
1条回答
  • 2020-12-07 04:46

    For std::vector<T> the elements are required to be Assignable. You type is not Assignable. An implementation of. std::vector<T> could avoid insisting on this requirement but this would be a disservice as the resulting code wouldn't be portable.

    You can use a std::list<T> instead or change the definition of you type. For example you can create an accessor to only read the value but no setter. Of course, assignment would change the value. The choice thus is to either allow this change or allow putting the objects into a container. You won't get both.

    0 讨论(0)
提交回复
热议问题