I have a virtual base class called ValuationFunction which basically just contains parameters and can output a value based on some calculation. All of the derived classes behave mostly the same way apart from one, which is called FunctionCombiner and looks like this:
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;
};
The way it works is that it contains a vector of pointers to other valuation function derived classes and for each of it's functions it's basically passing along the instructions to each of the inner classes it contains instead of doing any operation on its own. Now my problem is that I want to compare my valuationFunction classes to see if they are the same at one point, for all the derived classes apart from FunctionCombiner I can just directly compare the pointers, but for the FunctionCombiner I of course need the pointers to the Inner objects and not just the pointer to the FunctionCombiner which is all I seeem to have to work with. What would be a good way to get the addresses for the inner classes?
What I've tried is create a virtual function that returns a vector of the inner pointers (or itself) and then compile all of these vectors together to compare them, but this has mostly not compiled, inadvertently made copies, created lots of code where I repeat myself and have been straight up messy. My next idea (that I don't want to do) is to simply add a parameter to all my classes with a unique identifier that I will then retrieve and use for comparison instead of the addresses pointed to by the smart pointers. This solution definitely doesn't feel very elegant though and I would hope to be able to do it some way else.
EDIT with further explanation: The problem is that if I have a FunctionCombiner that contains Function A and Function B, and I compare it with Function A they will of course not be equal. When I compare Function A to FunctionCombiner I really want to compare Function A to Function A (inside function combiner) and Function A to Function B (inside function combiner). The end goal would be to exclude the duplicates so that after comparing Function A and FunctionCombiner I can remove one of the duplicare Function A.
Don't think it's really needed but valuationFunciton is below:
class valuationFunction
{
public:
valuationFunction(double TTM);
virtual void ValueInstrument() = 0;
virtual double GetValue() const;
virtual void RiskFactorAdd(double increment, RiskFactor simulatedRiskFactor) = 0;
virtual void RiskFactorMultiply(double factor, RiskFactor simulatedRiskFactor) = 0;
virtual void UpdateTTM(double timeStep);
virtual valuationFunction* clone() const = 0;
virtual ~valuationFunction() {}
private:
protected:
double f;
double TTM;
};
If I understand you correctly, you want to compare shared pointers to see if they contain the same stored pointer? Then it's trivial: just use the == operator
.
Internally operator==
for std::shared_ptr
is implemented as:
lhs.get() == rhs.get()
i.e. it checks the stored pointers are the same.
What you could do is use std::find()
to search for a particular std::shared_ptr<valuationFunction>
in your vector of "inner" objects. Also have a end()
function in FunctionCombiner
that you can compare for past end of vector iterator.
So your class definition will be something like this:
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;
};
Then you just need to call FindValuationFunction()
and compare with end()
. If FindValuationFunction()
doesn't equal end()
the shared pointer exists in the vector of inner objects.
I also changed the constructor to take vector by value and move it into Inner
. That will be more efficient.
Demo.
Hope this is what you are looking for.