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-tp7623p7629.html



Luigi Ballabio <[hidden email]> wrote on 02/09/2009 07:33:35 PM:

> On Wed, 2009-09-02 at 14:31 +1000, Yan Kuang wrote:
> > I am not sure I understand, do you mean point to a different object.
> > Shared pointer
> > can do too:
> >
> > void f (boost::shared_ptr<T> & pA)
> > {
> >       boost::shared_ptr<T> pB(new T);
> >
> >       pA = pB;
> > }        
>
> Yes, but try this:
>
> #include <iostream>
>
> class T {
>     int i_;
>   public:
>     T(int i) : i_(i) {}
>     int i() const { return i_; }
> };
>
> class Foo {
>     shared_ptr<T> p_;
>   public:
>     Foo(const shared_ptr<T>& p) : p_(p) {}
>     void check() const {
>         std::cout << p_->i() << std::endl;
>     }
> };
>
> int main() {
>     boost::shared_ptr<T> pA(new T(1));
>     Foo f(pA);
>     f.check();  // prints 1
>    
>     boost::shared_ptr<T> pB(new T(2));
>     pA = pB;    // you're changing pA, but not the copy
>                 // stored by the Foo object
>     f.check();  // in fact, this still prints 1
> }
>
This because constructor of Foo called copy constructor of boost::shared_ptr.

f.p_ is just a copy of pA.

So Handle behaves like a raw pointer. I can see YieldTermStructure, Quote etc using handle
in Quantlib code, I can see the reason now.

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