Posted by
Yan Kuang on
URL: http://quantlib.414.s1.nabble.com/default-constructor-again-tp7615p7617.html
Luigi Ballabio <[hidden email]> wrote
on 02/09/2009 07:42:54 PM:
> On Wed, 2009-09-02 at 10:12 +1000, Yan Kuang wrote:
> > I wrote a class (sorry I am still using excel date)
> >
> > class adtZeroCurve
> > {
> > public:
> > adtZeroCurve(std::vector<double>&
vDate, std::vector<double>&
> > vZeroRate,
> > doublevalDate)
> > {
> > m_ValDate=valDate;
> > //pdtInt
= boost::shared_ptr<LinearInterpolation>(new
> > LinearInterpolation(vDate.begin(), vDate.end(), vZeroRate.end()));
> > //pdtInt->update();
> > adtInt
=LinearInterpolation(vDate.begin(),
> > vDate.end(), vZeroRate.end());
> > }
> > virtual~adtZeroCurve();
> > doublegetDF(double aDate) {
> > doublezr
= adtInt(aDate);
> > returnpow(1
/ (1 + zr/2) ,((aDate -
> > m_ValDate)/365*2));
> > }
> >
> > private:
> >
> > //boost::shared_ptr<LinearInterpolation>
pdtInt;
> > LinearInterpolation adtInt;
> > doublem_ValDate;
> > };
>
> A note: LinearInterpolation doesn't copy the values, so you'll need
to
> store the two vectors as well to make sure that they are not deleted
and
> leave your interpolation dangling.
>
Thanks for the note!
> > As I am using getDF very, very often, for efficiency I just want
to
> > call LinearInterpolation constructor (copy) once.
>
> Of course.
>
> > But the above code
> > won't compile naturally until I add default constructor to
> > LinearInterpolation: LinearInterpolation(){}
>
> Most of your default-constructor problems will disappear if you use
> member initializers instead of assigning to members inside the
> constructor body. For example:
>
> class adtZeroCurve
> {
> private:
> std::vector<double> m_vDate;
> std::vector<double> m_vZeroRate;
> LinearInterpolation adtInt;
> double m_ValDate;
> public:
> adtZeroCurve(const std::vector<double>&
vDate,
>
std::vector<double>& vZeroRate,
>
double valDate)
> : m_vDate(vDate), m_vZeroRate(vZeroRate),
> adtInt(m_vDate.begin(), m_vDate.end(),
m_vZeroRate.begin()),
> m_valDate(valDate)
> {}
>
> virtual ~adtZeroCurve();
> double getDF(double aDate) {
> double zr
= adtInt(aDate);
> return pow(1
/ (1 + zr/2) ,((aDate - m_ValDate)/365*2));
> }
> };
Very neat indeed, thanks.
>
> > On one hand, this example may demonstrate
the need of a default
> > constructor. On the other hand I would like someone to point
> > out an alternative using boost::shared_ptr.
>
> That would be:
>
> class adtZeroCurve
> {
> private:
> std::vector<double> m_vDate;
> std::vector<double> m_vZeroRate;
> shared_ptr<LinearInterpolation>
adtInt;
> double m_ValDate;
> public:
> adtZeroCurve(const std::vector<double>&
vDate,
>
std::vector<double>& vZeroRate,
>
double valDate)
> : m_vDate(vDate), m_vZeroRate(vZeroRate),
> m_valDate(valDate)
> {
> adtInt = shared_ptr<LinearInterpolation>(
> new LinearInterpolation(
> m_vDate.begin(),
m_vDate.end(), m_vZeroRate.begin()));
> }
>
> double getDF(double aDate) {
> double zr
= (*adtInt)(aDate);
> return pow(1
/ (1 + zr/2) ,((aDate - m_ValDate)/365*2));
> }
> };
>
(*adtInt)(aDate):
I see, 'shared_ptr' have overloading operator *.
Many thanks,
Yan
Please consider our environment before printing this email.
WARNING - This email and any attachments may be confidential. If received in error, please delete and inform us by return email. Because emails and attachments may be interfered with, may contain computer viruses or other defects and may not be successfully replicated on other systems, you must be cautious. Westpac cannot guarantee that what you receive is what we sent. If you have any doubts about the authenticity of an email by Westpac, please contact us immediately.
It is also important to check for viruses and defects before opening or using attachments. Westpac's liability is limited to resupplying any affected attachments.
This email and its attachments are not intended to constitute any form of financial advice or recommendation of, or an offer to buy or offer to sell, any security or other financial product. We recommend that you seek your own independent legal or financial advice before proceeding with any investment decision.
Westpac Institutional Bank is a division of Westpac Banking Corporation, a company registered in New South Wales in Australia under the Corporations Act 2001 (Cth). Westpac is authorised and regulated in the United Kingdom by the Financial Services Authority and is registered at Cardiff in the United Kingdom as Branch No. BR 106. Westpac operates in the United States of America as a federally chartered branch, regulated by the Office of the Comptroller of the Currency.
Westpac Banking Corporation ABN 33 007 457 141.
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.
http://p.sf.net/sfu/bobj-july_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users