Re: default constructor again

Posted by Luigi Ballabio on
URL: http://quantlib.414.s1.nabble.com/default-constructor-again-tp7615p7616.html

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.

> 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));
        }
};

> 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));
        }
};

Luigi


--

Just remember what ol' Jack Burton does when the earth quakes, the
poison arrows fall from the sky, and the pillars of Heaven shake. Yeah,
Jack Burton just looks that big old storm right in the eye and says,
"Give me your best shot. I can take it."
-- Jack Burton, "Big trouble in Little China"



------------------------------------------------------------------------------
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