Re: Question about copy constructors

Posted by Luigi Ballabio on
URL: http://quantlib.414.s1.nabble.com/Question-about-copy-constructors-tp4027p4028.html

On 09/13/2005 11:40:34 PM, Chuck Hinman wrote:
> We see a need in the near future to make copies of QuantLib objects
> (yield term structures currently) from a repository for the purpose  
> of holding a yield term structure object constant during a large
> valuation of instruments while term structures in the repository is
> updated with new quotes. Can anyone tell me why the yield term
> structure classes have no copy contructor?

Chuck,
        because the copy constructor semantics is ambiguous---indeed,  
we should have disabled it altogether instead of letting the compiler  
generate one. The problem is, should the copy constructor return a term  
structure disconnected by the quotes used in the original, or one which  
is linked to the same quotes? Both are legitimate expectations...

In the meantime, a workaround you can use is to clone the term  
structure yourself. If you have a PiecewiseYieldCurve or  
PiecewiseFlatForward instance (depending on how you store them, you  
might have to downcast a YieldTermStructure) you can do it as follows:

DayCounter dayCounter = curve->dayCounter();
std::vector<Date> dates = curve->dates();
std::vector<DiscountFactor> discounts;
for (Size i=0; i<dates.size(); i++) {
     discounts.push_back(curve->discount(dates[i]));
}
boost::shared_ptr<YieldTermStructure> clone(
     new InterpolatedDiscountCurve(dates,discount,
                                   dayCounter));

Later,
        Luigi



----------------------------------------

Newton's Law of Gravitation:
        What goes up must come down.  But don't expect it to come down
        where you can find it.  Murphy's Law applies to Newton's.