Re: reference of boost::shared_ptr instead of Handle

Posted by Luigi Ballabio on
URL: http://quantlib.414.s1.nabble.com/reference-of-boost-shared-ptr-instead-of-Handle-tp7623p7628.html

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
}


With Handles instead, when you relink the handle the Foo instance sees
the new T object.

Luigi


--

Skinner's Constant (or Flannagan's Finagling Factor):
That quantity which, when multiplied by, divided by, added to,
or subtracted from the answer you got, gives you the answer you
should have gotten.



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