想出解决 C++ 中循环引用的问题
Trouble coming up with a solution to a circular reference in C++
我有两个基础 类,valuationFunction 和 SimulationEngine,我相信我在其中创建了一个循环引用,我不确定如何整理。最初我只让 SimulationEngine 包含 ValuationFunction,但后来我在 SimulationEngine 中创建了枚举 RiskFactor
,我需要 ValuationFunction 来识别,这就是我认为我的问题所在。仅仅看一下这显然是错误的,因为两个头文件管理器相互包含,但是我如何才能获得 ValuationFunction(以及所有继承的 类)能够获取类型为 [=12= 的对象] 作为输入?
模拟引擎:
#pragma once
#define SIMULATION_ENGINE_H
#include "valuationFunction.h"
#include "Wrapper.h"
class SimulationEngine
{
public:
enum RiskFactor { interest_rate, equity, volatility, FX_rate };
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();
Wrapper<valuationFunction>& GetFunction();
RiskFactor simulatedRiskFactor;
protected:
double horizon;
Wrapper<valuationFunction> theFunction;
};
估值函数:
#pragma once
#define VALUATION_FUNCTION_H
#include "SimulationEngine.h"
class valuationFunction
{
public:
valuationFunction(double TTM);
virtual void ValueInstrument() = 0;
virtual double GetValue() const;
virtual void RiskFactorAdd(double increment, SimulationEngine::RiskFactor simulatedRiskFactor) = 0;
virtual void RiskFactorMultiply(double factor, SimulationEngine::RiskFactor simulatedRiskFactor) = 0;
virtual void UpdateTTM(double timeStep);
virtual valuationFunction* clone() const = 0;
virtual ~valuationFunction() {}
private:
protected:
double f;
double TTM;
};
创建RiskFactor.h。把枚举放在那里。将其包含在 headers.
中
我有两个基础 类,valuationFunction 和 SimulationEngine,我相信我在其中创建了一个循环引用,我不确定如何整理。最初我只让 SimulationEngine 包含 ValuationFunction,但后来我在 SimulationEngine 中创建了枚举 RiskFactor
,我需要 ValuationFunction 来识别,这就是我认为我的问题所在。仅仅看一下这显然是错误的,因为两个头文件管理器相互包含,但是我如何才能获得 ValuationFunction(以及所有继承的 类)能够获取类型为 [=12= 的对象] 作为输入?
模拟引擎:
#pragma once
#define SIMULATION_ENGINE_H
#include "valuationFunction.h"
#include "Wrapper.h"
class SimulationEngine
{
public:
enum RiskFactor { interest_rate, equity, volatility, FX_rate };
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();
Wrapper<valuationFunction>& GetFunction();
RiskFactor simulatedRiskFactor;
protected:
double horizon;
Wrapper<valuationFunction> theFunction;
};
估值函数:
#pragma once
#define VALUATION_FUNCTION_H
#include "SimulationEngine.h"
class valuationFunction
{
public:
valuationFunction(double TTM);
virtual void ValueInstrument() = 0;
virtual double GetValue() const;
virtual void RiskFactorAdd(double increment, SimulationEngine::RiskFactor simulatedRiskFactor) = 0;
virtual void RiskFactorMultiply(double factor, SimulationEngine::RiskFactor simulatedRiskFactor) = 0;
virtual void UpdateTTM(double timeStep);
virtual valuationFunction* clone() const = 0;
virtual ~valuationFunction() {}
private:
protected:
double f;
double TTM;
};
创建RiskFactor.h。把枚举放在那里。将其包含在 headers.
中