4

I'm trying to implement this logic in C++:

Object obj(args);
while (obj.isOK()) {
    obj = obj.next();
}

But I can't use this exact code because Object inherits boost::noncopyable so it has no assignment operator. I can add methods and constructors to Object, (but not make it copyable), however I would prefer not to. Other questions have manual destruction and placement new as a solution, which I could do if I create a new constructor for Object, but again, preferably I wouldn't need the new constructor, and that seems like a pretty nasty solution anyway. What alternatives do I have?

Drew
  • 12,578
  • 11
  • 58
  • 98

2 Answers2

3

Make Object::next mutate the Object in place. Since Object is not copyable, this seems like the only sensible thing for Object::next to do anyway.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
-1

What does Object::next return? If it returns a fresh Object you could add a move-assignment operator to Object:

Object& operator=(Object&& obj)
{
    //move its iternal state to this and then
    return *this;
}

So no data duplication takes place and nothing is actually copied.

TheROE
  • 134
  • 3