简体   繁体   中英

move assignment to object with const value

I have a struct like this:

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

And an example code

void func() {
  static OBJ obj;
  OBJ other; // random values
  if(conditon)
    obj = std::move(other); //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.

You're doing constructors wrong. Constructors should initialize , not assign:

OBJ(OBJ &&oth) : y(oth.y) {}
//             ^^^^^^^^^^

Also, constructors cannot return *this , since they have no return type.

An assignment operator for your class doesn't make sense since the class has unassignable members (namely constants). (You could of course write a custom assignment that doesn't modify the const member, but then you'd have a truly weird class that has extremely surprising behaviour.)

What about writing the move-assignment operator this way:

OBJ& operator=(OBJ&& other) {
    this->~OBJ();
    new(this) OBJ(other.x, other.y);
    return *this;
}

You would also need a constructor:

OBJ(const int x, const int y)
    : x(x), y(y)
{
}

I would suggest moving to std::unique_ptr :

void func() {
  static std::unique_ptr<OBJ> obj = std::make_unique<OBJ>();
  std::unique_ptr<OBJ> other = std::make_unique<OBJ>(); // random values
  if(condition)
    obj = std::move(other); //move
}

This should be your choice in many cases where there is a need to move something that cannot be moved, to hold an unknown polymorphic type or any other case where you cannot deal with the actual type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM