Posted by
Yan Kuang on
URL: http://quantlib.414.s1.nabble.com/reference-of-boost-shared-ptr-instead-of-Handle-tp7623p7631.html
In this example, I can use reference so that the second
f.check() print 2.
The only thing to notice is p_ is an alias, can not
be change to something else.
#include <boost/shared_ptr.hpp>
#include <iostream>
class T {
int
i_;
public:
T(int
i) : i_(i) {}
int
i() const { return
i_; }
};
class Foo {
boost::shared_ptr<T> & p_;
public:
Foo(boost::shared_ptr<T>&
p) : p_(p) {}
void
check() const {
std::cout << p_->i()
<< std::endl;
}
};
int _tmain(int
argc, _TCHAR* argv[]) {
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(); //
now print 2.
}
On Thu, 2009-09-03 at 09:41 +1000, Yan Kuang wrote:
> Luigi Ballabio <[hidden email]> wrote on 02/09/2009
07:33:35
> PM:
> > Yes, but try this:
> >
> This because constructor of Foo called copy constructor of
> boost::shared_ptr.
That's the same with raw pointers. Look at the following, and forget
about the fact that I'm leaking memory:
#include <iostream>
class T {
int i_;
public:
T(int i) : i_(i) {}
int i() const { return i_; }
};
class Foo {
T* p_;
public:
Foo(T* p) : p_(p) {}
void check() const {
std::cout << p_->i() << std::endl;
}
};
int main() {
T* pA = new T(1);
Foo f(pA);
f.check(); // prints 1
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
}
> So Handle behaves like a raw pointer.
Handle behaves like a pointer to pointer. If you're reading appendix
A
of my drafts, look at the aside on pointer semantics.
Luigi
--
Never mistake motion for action.
-- Ernest Hemingway
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