Re: reference of boost::shared_ptr instead of Handle

Posted by Yan Kuang on
URL: http://quantlib.414.s1.nabble.com/reference-of-boost-shared-ptr-instead-of-Handle-tp7623p7625.html



Luigi Ballabio <[hidden email]> wrote on 27/08/2009 06:30:35 PM:

> On Thu, 2009-08-27 at 17:14 +1000, Yan Kuang wrote:
> > First of all, I found some classes of Quantlib have no default
> > constructor. This mean users have to create object on the heap.
>
> How are the two things related? You can create objects on the stack just
> the same. You'll just have to pass arguments to the constructor instead
> of just declaring the object.
>
Sorry I make the confusion. I mean without default constructor, you can't declare

an object without passing argument, but you can declare a pointer.

> > Is this intentional, as I tentatively want to add default constructor
> > for the class I am using.
>
> If there's no default constructor, it probably makes no sense to have
> one. The default-constructed object wouldn't have enough information to
> work anyway.
>

It's just during development, sometimes I like to declare an object without passing
the argument first, write follow on routine and test the compilation. I change the
default constructor to a specialised constructor a little late on.
 
>
> > Anyway in the case of no default constructor, we use
> > boost::shared_ptr. I read appendixA.pdf of Quantlib doc: Handle is
> > designed
> > to replace pointer to pointer. But I think passing reference of
> > boost::shared_ptr in a function call will have same effect. I could be
> > wrong,
> > any comments?
>
> I'm not sure I understand. If the function takes a Handle, you'll have
> to use one since there's no automatic conversion from boost::shared_ptr.
> Or do you want to write the function instead?
>
> Luigi
>

Take an example:

class adtLGM  
{
public:
        adtLGM();
       
        adtLGM(int rollNum, std::vector<double> Roll, std::vector<double> exBool, std::vector<double> notional,  
                std::vector<double> FixedSched,std::vector<double> Rmax, std::vector<double> Rmin, int pay1_rec0,
                string liborTerm, int rollRule, string Hol, int daycount, boost::shared_ptr<SwaptionVolCube2>& swopVolCube,
                boost::shared_ptr<CapFloorTermVolSurface>& cpflVolSurf);

        virtual ~adtLGM();

        double LGM_CpflrLetPDE(int call_put, int expIdx, int N, int M);

private:
               
.......................... deleted ........
       
        boost::shared_ptr<SwaptionVolCube2> m_swopVolCube;
        boost::shared_ptr<CapFloorTermVolSurface> m_cpflVolSurf;
.......................... deleted .......        
};


adtLGM::adtLGM(int rollNum, std::vector<double> Roll, std::vector<double> exBool, std::vector<double> notional,  
                std::vector<double> FixedSched,std::vector<double> Rmax, std::vector<double> Rmin, int pay1_rec0,
                string liborTerm, int rollRule, string Hol, int daycount, boost::shared_ptr<SwaptionVolCube2>& swopVolCube,
                boost::shared_ptr<CapFloorTermVolSurface>& cpflVolSurf)
{
       
.......................... deleted ........

        m_swopVolCube = swopVolCube;
        m_cpflVolSurf = cpflVolSurf;

.......................... deleted ........

}

....................

Suppose suppose I create SwaptionVolCube2 object, then I pass the shared_ptr (as reference) to
adtLGM constructor. If I purtube SwaptionVolCube2 object after an adtLGM object construction,
I expect m_swopVolCube reflect the pertubation. Am I right? (I haven't tested).

In Quantlib doucument appendixA.pdf, it is stated:



I thought this paragraph implies Handle class is designed to replace smart pointer to smart pointer.
While I thought we never needed a smart pointer to smart pointer, we should use the reference passing instead.

I never used boost::shared_ptr before, so I could be wrong. Or I misunderstood the document?

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