How to make an enum recognized by other classes

本小妞迷上赌 提交于 2021-01-29 13:35:25

问题


I'm creating an enum called RiskFactor in my SimulationEngine base class.

class SimulationEngine
{
public:
    enum RiskFactor { interest_rate, equities, volatility };
    SimulationEngine(double horizon, Wrapper<valuationFunction>& theFunction_, RiskFactor simulatedRiskFactor);
    virtual void DoOnePath(double vol, double normvariate) = 0;
    virtual SimulationEngine* clone() const = 0;
    const virtual double GetHorizon();
    const Wrapper<valuationFunction>& GetFunction() const;
    RiskFactor simulatedRiskFactor;
protected:
    double horizon;
    Wrapper<valuationFunction> theFunction;
};

In a derived class I have this method, using the "simulatedRiskFactor" of type RiskFactor enum when calling a method of the object "TheFunction".

void OneStepBSEngine::DoOnePath(double vol, double normvariate)
{
    double variance = vol * vol * horizon;
    double rootVariance = sqrt(variance);
    double itoCorrection = -0.5 * variance;
    //double movedSpot = spotvalue * exp(drift * horizon + itoCorrection);
    //spotvalue = movedSpot * exp(rootVariance * normvariate);
    double factor = exp(drift * horizon + itoCorrection + rootVariance * normvariate);
    theFunction->RiskFactorMultiply(factor, simulatedRiskFactor);
    return;
}

How should I make the class "theFunction" look for it to recognize the enum and allow me to write the last line (not yet working):

theFunction->RiskFactorMultiply(factor, simulatedRiskFactor);

The class looks like this currently:

class valuationFunction
{
public:
    valuationFunction(double TTM);
    virtual void ValueInstrument() = 0;
    virtual double GetValue() const;
    virtual void RiskFactorAdd(double increment) = 0;
    virtual void RiskFactorMultiply(double factor) = 0;
    virtual void UpdateTTM(double timeStep);
    virtual valuationFunction* clone() const = 0;
    virtual ~valuationFunction() {}
private:

protected:
    double f;
    double TTM;
};

And I want to be able to call the RiskFactorAdd and RiskFactorMultiply functions with the enum RiskFactor.


回答1:


The declaration of RiskFactorMultiply needs to qualify the enum name with the enclosing class name.

class valuationFunction
{
// ...
    virtual void RiskFactorMultiply(double factor, SimulationEngine::RiskFactor risk) = 0;
// ...
};

The same applies to the enum values, if for example RiskFactorMultiply wanted to make the second argument optional.

    virtual void RiskFactorMultiply(double factor, SimulationEngine::RiskFactor risk = SimulationEngine::volatility) = 0;


来源:https://stackoverflow.com/questions/61896004/how-to-make-an-enum-recognized-by-other-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!