Posted by
Luigi Ballabio-2 on
Oct 26, 2002; 8:43am
URL: http://quantlib.414.s1.nabble.com/call-for-void-constructors-tp10165p10169.html
Hi Vadim,
At 7:16 PM -0500 10/25/02, Vadim Ogranovich wrote:
>I find it inconvenient that FdDividendAmericanOption, etc. have no void
>constructor. I was working around the problem of zero dividend time and
>wanted to overload FdDividendAmericanOption with a new class,
>FdDividendAmericanOption2, that will chop off the zero time dividend and
>pass only the remaining dividends to FdDividendAmericanOption.
>
>However the problem in actually writing
>FdDividendAmericanOption2::FdDividendAmericanOption2 is that I have very
>limited "coding space" to check whether I need to chop off the first element
>and, if yes, to actually chop it off. Indeed, all these calculations need to
>happen before the base class, FdDividendAmericanOption, gets initialized by
>its constructor.
Well, not really. The constructor of FdDividendAmericanOption doesn't
perform any calculation---it just stores the input data. Calculations
are done when first asked for and take place inside
FdDividendAmericanOption::calculate().
You can override the latter in your derived pricer and write:
class FdDividendAmericanOption2 : public FdDividendAmericanOption {
public:
// constructor
FdDividendAmericanOption2(Option::Type type, double underlying,
double strike, Spread dividendYield,
Rate riskFreeRate,
Time residualTime, double volatility,
const std::vector<double>& dividends =
std::vector<double>(),
const std::vector<Time>& exdivdates =
std::vector<Time>(),
int timeSteps = 100, int gridPoints = 100);
void calculate() const;
};
FdDividendAmericanOption2::FdDividendAmericanOption2(
Option::Type type, double underlying,double strike, Spread dividendYield,
Rate riskFreeRate, Time residualTime, double volatility,
const std::vector<double>& dividends, const std::vector<Time>& exdivdates,
int timeSteps, int gridPoints)
: FdDividendAmericanOption(type,underlying,strike,dividendYield,riskFreeRate,
residualTime,volatility,dividends,exdivdates,
timeSteps,gridPoints)
{}
void FdDividendAmericanOption2::calculate() const {
if (we need to discard the first element) {
chop it off;
}
FdDividendAmericanOption::calculate();
}
You might need to add "mutable" to the data members you'll modify in
the above method.
As for the default constructor issue, the only problem I see is that
a constructor should leave the instance in a useable state, or at
least in a state in which the instance knows it wasn't properly
initialized so that it will raise an exception instead of choking
when asked its value. As for me, I found that most of the times, the
hassle of having to introduce an initialization flag and checking it
in every method is not worth the added addvantage of having a void
constructor. But that's just my opinion.
Hope this helped,
Luigi