如何使枚举被其他班级认可 [英] How to make an enum recognized by other classes

查看:46
本文介绍了如何使枚举被其他班级认可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SimulationEngine基类中创建一个名为RiskFactor的枚举.

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

在派生类中,我有此方法,在调用对象"TheFunction"的方法时使用RiskFactor枚举类型的"simulatedRiskFactor".

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

我应该如何使"theFunction"类寻找它来识别枚举并允许我编写最后一行(尚不可用):

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

该类当前如下所示:

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

我希望能够使用枚举RiskFactor调用RiskFactorAdd和RiskFactorMultiply函数.

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

推荐答案

RiskFactorMultiply 的声明需要使用包含的类名来限定枚举名称.

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

对于枚举值也是如此,例如,如果 RiskFactorMultiply 想要使第二个参数为可选.

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;

这篇关于如何使枚举被其他班级认可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆