vendredi 8 septembre 2017

move assigment to object with const value

I have a struct like this:

struct OBJ {
  int x;
  const int y;
  OBJ& OBJ(OBJ &&oth)
  {
    y = oth.y; // this is disallowed
    return *this;
  }
}

And an example code

void func() {
  static OBJ obj;
  if(conditon)
    obj = std::move(OBJ()); //move
}

I understand this as obj is Non const OBJ with const member y. I can't change just y but I should be able to change whole object (call destructor and constructor). Is this possible or the only proper solution is to remove my const before y, and remember to don't change by accident?

I need to store my static obj between func call but if condition is true i want to move other object in place of this static object.

Aucun commentaire:

Enregistrer un commentaire