基本上我想这样做:
Object obj;
while (app->running)
obj = update(obj)
其中update是一种函数,在某些情况下返回一个新的Object,而在其他情况下返回相同的对象,不变:
Object update(const Object& obj) {
if (something)
return Object{/*params*/};
else
return obj;
我的问题是如何让Object
仅在返回新对象时执行所有复制赋值操作,但在返回初始obj
时不执行任何操作?
答案 0 :(得分:1)
一种方法是启用Object
类的移动,并将对象移入和移出update
函数。
答案 1 :(得分:0)
我会像其他人提到的那样建议类似!important
的东西,或者最好是可以在任何对象上调用的成员void update(Object& obj)
。
update
请注意,检查class Object {
public:
//other stuff
bool update() {
if (something) {
//update stuff
return true;
else return false;
}
}
中的自我分配并从operator=
返回引用不起作用,因为返回新对象时悬挂引用。