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

Posted by WilliamKF on Stack Overflow See other posts from Stack Overflow or by WilliamKF
Published on 2012-03-24T17:16:09Z Indexed on 2012/03/24 17:29 UTC
Read the original article Hit count: 174

Filed under:
|
|
|

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 value)
    : _value(value) {
  }
  const int _value;
}

vector<Item> items;

items.push_back(Item(3));

I'd like to keep the _value const since it should not change after the object is constructed, so the question is how do I initialize my vector with elements without invoking operator=()?

© Stack Overflow or respective owner

Related posts about c++

Related posts about const