我有一个类functionCombiner,它的构造函数是这样的。
FunctionCombiner::FunctionCombiner(std::vector<std::shared_ptr<valuationFunction>> Inner_) : Inner(std::move(Inner_)), valuationFunction("", 0) //<- Needs to initalize itself even though it gets all data from inner functions.
{
}
在我的主类中,我是这样调用的。
vector<std::shared_ptr<valuationFunction>> combinedStillFrontFunctions{ stillFrontStock, stillFrontEuropeanCall };
std::shared_ptr<valuationFunction> StillFrontFunctions = std::make_shared<FunctionCombiner>(combinedStillFrontFunctions);
我想做的是把它缩减为一行 就像这样构造它的位置
std::shared_ptr<valuationFunction> StillFrontFunctions = std::make_shared<FunctionCombiner>({ stillFrontStock, stillFrontEuropeanCall });
编译器不喜欢的。有什么办法可以让它工作吗?这显然是可行的
FunctionCombiner StillFrontFunctions({ stillFrontStock, stillFrontEuropeanCall });
但我需要它是一个共享指针。
(缩短一些名字是为了让这个例子在水平滚动条上可读。 你也应该考虑这样做...)
传递 {x,y}
到 make_shared()
是试图转发一个括号包围的初始化列表,不是初始化共享指针中的值,而是初始化其构造函数所取的临时对象。 由于它本身不是一个完整的表达式,所以不是可以转发的东西。 所以用这些值做一个临时向量。
... = make_shared<FunComb>(vector<shared_ptr<valFun>>{FS, FEC});
另一种方法可能是改变 FunComb
的构造函数成为一个(或添加一个新的)变量构造函数,这样就不需要传递一个 vector
只是为了保持输入。