Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 正在检索“的地址”;“内部”;类中的类_C++_Shared Ptr_Smart Pointers_Memory Address - Fatal编程技术网

C++ 正在检索“的地址”;“内部”;类中的类

C++ 正在检索“的地址”;“内部”;类中的类,c++,shared-ptr,smart-pointers,memory-address,C++,Shared Ptr,Smart Pointers,Memory Address,我有一个虚拟的基类叫ValuationFunction,它基本上只包含参数,可以根据一些计算输出一个值。除了一个名为FunctionCombiner的派生类外,所有派生类的行为基本相同,如下所示: class FunctionCombiner : public valuationFunction { public: FunctionCombiner(std::vector<std::shared_ptr<valuationFunction>>& Inner

我有一个虚拟的基类叫ValuationFunction,它基本上只包含参数,可以根据一些计算输出一个值。除了一个名为FunctionCombiner的派生类外,所有派生类的行为基本相同,如下所示:

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

    FunctionCombiner(std::vector<std::shared_ptr<ValuationFunction>> Inner_) : Inner(std::move(Inner_)) {}
    virtual ~FunctionCombiner() = default;

    iterator FindValuationFunction(const std::shared_ptr<ValuationFunction>& fn) {
        return std::find(Inner.begin(), Inner.end(), fn);
    }

    iterator end() { return Inner.end(); }

private:
    std::vector<std::shared_ptr<ValuationFunction>> Inner;
};

如果我理解正确,您想比较共享指针,看看它们是否包含相同的存储指针?这样就很简单了:只需使用
==运算符

内部for
std::shared_ptr
实现为:

lhs.get() == rhs.get()
i、 e.检查存储的指针是否相同


您可以使用
std::find()
在“内部”对象的向量中搜索特定的
std::shared\u ptr
。在
FunctionCombiner
中还有一个
end()
函数,您可以比较过去的向量迭代器末尾

因此,您的类定义如下:

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

    FunctionCombiner(std::vector<std::shared_ptr<ValuationFunction>> Inner_) : Inner(std::move(Inner_)) {}
    virtual ~FunctionCombiner() = default;

    iterator FindValuationFunction(const std::shared_ptr<ValuationFunction>& fn) {
        return std::find(Inner.begin(), Inner.end(), fn);
    }

    iterator end() { return Inner.end(); }

private:
    std::vector<std::shared_ptr<ValuationFunction>> Inner;
};
类函数组合器:公共赋值函数
{
公众:
typedef std::vector::迭代器迭代器;
FunctionCombiner(std::vector内部):Inner(std::move(内部){
virtual~FunctionCombiner()=默认值;
迭代器FindValuationFunction(const std::shared_ptr&fn){
返回std::find(Inner.begin(),Inner.end(),fn);
}
迭代器end(){return Inner.end();}
私人:
std::向量内部;
};
然后只需调用
FindValuationFunction()
并与
end()
进行比较。如果
FindValuationFunction()
不等于
end()
共享指针存在于内部对象的向量中

我还将构造函数更改为按值获取向量,并将其移动到
内部。这样会更有效率


希望这就是你想要的。

你所说的平等到底是什么意思?引用/指针相等?然后比较指针;这个
函数组合器
没有什么特别之处。价值平等?然后功能平等?这基本上是无法解决的:)旁注:构造函数的引用
std::vector
参数是可疑的。参数可能应按值取值,并将
std::move
'd移到数据成员中。@Thomas Hi,yes指针相等。问题是,如果我有一个包含函数a和函数B的FunctionCombiner,我将其与函数a进行比较,它们当然不相等。当我比较函数A和函数组合器时,我真的想比较函数A和函数A(内部函数组合器),函数A和函数B(内部函数组合器)。最终目标是排除重复项,以便在比较函数A和函数组合器之后,我可以删除其中一个duplicare函数A。希望这能使它更清晰。问题是,我有一个指向对象的共享指针,而该对象又包含指向更多“内部”对象的指针。因此,我想做的是能够将指针与内部对象进行比较,而不是与原始对象(FunctionCombiner)进行比较。正如我提到的,我尝试创建函数来检索向量中的内部对象,然后尝试将这些向量相互比较,就像你所说的,但是它变得非常复杂,似乎不想用于me@Oscar所以你想迭代内部对象,寻找一个特定的函数,一个对象?这些内部对象可能是其他
FunctionCombiner
对象吗?没错!不,我不需要考虑这些