【问题标题】:Returning a vector of references from a Class从类返回引用向量
【发布时间】:2020-09-17 22:53:41
【问题描述】:

我有一个名为 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&lt; valuationFunction&amp;&gt; 的内容,但编译器似乎不太喜欢这样。解决这个问题的正确方法是什么?

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

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

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;
}

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

【问题讨论】:

标签: c++ vector reference


【解决方案1】:

要将引用存储在 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';    
}

【讨论】:

  • 嗨,谢谢你,我尝试实现这个,但它仍然没有编译并给我一百万个错误,你能看看我的编辑,看看我是否理解正确或做错了什么?跨度>
  • @Oscar 好的,我会做一个更接近您想要实现的示例
  • @Oscar 您的问题的一部分是您试图通过vector 的构造函数默认构造std::reference_wrapper。你不能默认构造一个std::reference_wrapperen.cppreference.com/w/cpp/utility/functional/reference_wrapper/…
  • @Oscar 我还从第一个示例中删除了std::ref,因为在这种情况下不需要helper 函数,因为我可以使用std::reference_wrapper 构造函数。
  • 谢谢!你是对的,罪魁祸首是它没有让我通过向量的构造函数默认构造reference_wrapper(为什么我不允许这样做(或者为什么编译器不能让我知道)更实际的方式啊)是另一个单独的问题)。不过,非常感谢您的帮助。我会再看几次你的答案,希望我能更好地理解这一切是如何结合在一起的
猜你喜欢
  • 1970-01-01
  • 2023-01-04
  • 1970-01-01
  • 2019-06-11
  • 2023-04-11
  • 1970-01-01
  • 2020-07-06
  • 2014-12-16
  • 1970-01-01
相关资源
最近更新 更多