问题
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