【发布时间】:2015-06-12 21:23:31
【问题描述】:
我的代码如下所示:
ISessionUpdater* updater = nullptr;
if (eventName == "test")
updater = new TestJSONSessionUpdater(doc);
if (eventName == "plus")
updater = new PlusJSONSessionUpdater(doc);
if (updater)
{
bool result = updater->update(data);
delete updater;
return result;
}
return false;
除了unique_ptr,有什么办法可以做这样的事情吗?
也就是说,只有 1 次调用 update(data) 而不是:
if(cond)
make unique
call update
end
if(cond)
make unique
call update
end
...
【问题讨论】:
-
unique_ptr 有方法重置来改变它存储的指针,如果你需要的话。
-
我的意思是,我可以在堆栈上分配一个空白的 unique_ptr,我的一个 if 填充它,然后如果它被一个 if 填充,则执行更新函数。
-
是的,照字面意思做一样......?
标签: c++ c++11 smart-pointers unique-ptr