Search code examples
c++initializationnoncopyable

C++ - how to reinitialize object repeatedly?


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?


Solution

  • 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.