|
From: Luigi B. <lui...@gm...> - 2008-02-25 16:43:22
|
Hi Simon, On Fri, 2008-02-15 at 09:57 +0000, Simon Ibbotson wrote: > I'm a little confused about the QuantLib curve classes ForwardCurve, > DiscountCurve and ZeroCurve. > > In the three class definitions we have the member variable > mutable Interpolation interpolation_; > > [...] often the object allocated is an object of a derived class e.g. > CubicSpline. > > Now, I know pointers and references can be polymorphic. But in this > case a derived class is being allocated to a base class instance... > > I know most information required for interpolation is contained within > the Interpolation::impl_ object but I'm wondering whether: > > a) my C++ knowledge is lacking and the base class instance (e.g. > ForwardCurve::interpolation_) can be polymorphic somehow. Or... No, you're right---it's not polymorphic. As you surmised, the polymorphic behavior is delegated to the inner Interpolation::impl_ object, which is copied by pointer and continues to have the correct type even when the containing Interpolation object is sliced. > b) why a pointer to the derived Interpolation object isn't returned by > the factory class (e.g. Cubic::interpolate) - to obviate the need for > a polymorphic Interpolation::impl_ member variable? I confess that it's a while since we coded it, so I might be wrong. But I think it's the same reason why we coded, say, the Calendar and DayCounter classes in the same way---to avoid writing the shared_ptr part. See <http://quantlib.org/quep/quep001.html> for the full rationale (the code samples are outdated, but the reasoning still holds.) Luigi -- Olmstead's Law: After all is said and done, a hell of a lot more is said than done. |