从类返回引用向量

Returning a vector of references from a Class

本文关键字:向量 引用 返回      更新时间:2023-10-16

我有一个名为FunctionCombiner的函数(基本类型ValuationFunction(,其中包含其他估值函数,如下所示:

class FunctionCombiner : public valuationFunction
{
public:
FunctionCombiner(std::vector<std::shared_ptr<valuationFunction>>& Inner_);
void ValueInstrument();
std::vector<std::string> GetuniqueIdentifier() const;
void RiskFactorAdd(double increment, RiskFactor simulatedRiskFactor);
void RiskFactorMultiply(double factor, RiskFactor simulatedRiskFactor);
virtual valuationFunction* clone() const;
private:
std::vector<std::shared_ptr<valuationFunction>> Inner;
};

我需要做的是创建一个函数来返回对这些"内部"函数的引用,但我不确定该怎么做,我尝试过的所有内容似乎在语法上都失败了。如果我只返回一个引用(不在向量内(,我会像这样做:

valuationFunction& FunctionCombiner ::GetInner()
{
return *this;
}

我尝试返回类似std::vector< valuationFunction&>的东西,但编译器似乎不太喜欢这样。正确的方法是什么?

最终目标是收集所有这些对内部函数的引用(来自多个 FunctionCombiner 或不同的评估函数(,以便比较它们并在以后整理重复项。

编辑:从答案中,我现在实现了返回内部引用的函数,如下所示: 对于"内部"类,我的函数现在看起来像这样:

std::vector<std::reference_wrapper<valuationFunction>> StockPriceFunction::GetInnerReference()
{
std::vector<std::reference_wrapper<valuationFunction>> innerVector(1);
innerVector.push_back(std::ref(*this));
return innerVector;
}

就像组合器一样:

std::vector<std::reference_wrapper<valuationFunction>> FunctionCombiner::GetInnerReference()
{
std::vector<std::reference_wrapper<valuationFunction>> innerVector(Inner.size());
for (unsigned long i = 0; i < Inner.size(); ++i) {
std::vector<std::reference_wrapper<valuationFunction>> innerInnerVector = Inner[i]->GetInnerReference();
innerVector.insert(innerVector.end(), innerInnerVector.begin(), innerInnerVector.end());
}
return innerVector;
}

但它不是编译并给我奇怪的错误,我在这里做错了什么?

为了在std::vector中存储引用,您必须使用 std::reference_wrapper。

#include <functional>
#include <vector>
int main( )
{
std::vector<int> ints{ 1, 2, 3, 4, 5 };
std::vector<std::reference_wrapper<int>> references;
for( auto& i : ints )
references.push_back( i );
auto& ref{ references[ 1 ].get( ) };
ref += 20;
// Prints 22.
std::cout << ints[ 1 ] << 'n';
}

这是另一个示例,它更接近您正在尝试执行的操作(我还包括了您需要的标头(。

#include <iostream>
#include <functional>
#include <vector>
#include <memory>
struct ValuationFunction 
{ 
ValuationFunction( int prop ) 
: property{ prop } { }
int property; 
};

// Converts the vector of pointers to a vector 
// of references.
static std::vector<std::reference_wrapper<ValuationFunction>> 
ToReference( const std::vector<std::shared_ptr<ValuationFunction>>& pointers )
{
std::vector<std::reference_wrapper<ValuationFunction>> references;
references.reserve( pointers.size( ) );
for ( auto& pointer : pointers )
references.emplace_back( *pointer );
return references;
}
int main( )
{
std::vector<std::shared_ptr<ValuationFunction>> pointers
{
std::make_shared<ValuationFunction>( 1 ),
std::make_shared<ValuationFunction>( 2 ),
std::make_shared<ValuationFunction>( 3 ),
std::make_shared<ValuationFunction>( 4 )
};
auto references{ ToReference( pointers ) };
// Use 'std::reference_wrapper.get( )' to get
// access to the contained reference.
for ( auto& ref : references )    
std::cout << "Before: " << ref.get( ).property << 'n';
// Change the property value of each 'ValuationFunction'
// in the original list.
for ( auto& pointer : pointers )
pointer->property += 10;
// Re-print the property value of each reference and see
// how they have all increased by 10.
for ( auto& ref : references )    
std::cout << "After: " << ref.get( ).property << 'n';    
}