use*_*275 3 c++ pointers if-statement allocation
我正在研究一些函数,这个函数,根据一些参数,可能需要一个Model对象.Model对象非常大,我不想在不需要时分配一个.从本质上讲,这就是我想要做的事情:
Model *myModel;
if (modelIsNeeded(arguments)) {
myModel = &Model(arguments);
}
//processing ...
Run Code Online (Sandbox Code Playgroud)
我有错误 error: taking address of temporary [-fpermissive]
你看到任何解决方法吗?做我想做的事的C++方式是什么?
你看到任何解决方法吗?做我想做的事的C++方式是什么?
改为使用智能指针:
std::unique_ptr<Model> myModel;
if (modelIsNeeded(arguments)) {
myModel = std::make_unique<Model>(arguments);
}
Run Code Online (Sandbox Code Playgroud)