c ++:在if中分配一个变量

c++: allocating a variable inside a if

我正在处理一些功能,这个功能,根据一些参数,可能需要一个 Model 对象。 Model 对象很大,我不想在不需要时分配一个。本质上,这就是我想要做的:

Model *myModel;
if (modelIsNeeded(arguments)) {
    myModel = &Model(arguments);
}

//processing ...

我有错误error: taking address of temporary [-fpermissive]

您看到任何解决方法了吗?做我想做的事情的 C++ 方法是什么?

Do you see any workaround? What is the C++ way of doing what I want to do?

改用smart pointer

std::unique_ptr<Model> myModel;
if (modelIsNeeded(arguments)) {
    myModel = std::make_unique<Model>(arguments);
}