Style guide on pointers

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Style guide on pointers

Joseph Wang
Could someone add to the wiki or the web, a style guide on pointers.  
I'm a little confused over what Handle is being used for and if this is
prefered over the boost pointers or not.




Reply | Threaded
Open this post in threaded view
|

Re: Style guide on pointers

Luigi Ballabio
On 04/22/05 08:00:04, Joseph Wang wrote:
> Could someone add to the wiki or the web, a style guide on pointers.  I'm  
> a little confused over what Handle is being used for and if this is  
> prefered over the boost pointers or not.

They serve two different purposes. A shared_ptr is the smart equivalent of  
a pointer. A Handle is the equivalent of a pointer to pointer. The  
difference is as follows:

class Foo {};

class Bar {
     boost::shared_ptr<Foo> f_;
   public:
     Bar(boost::shared_ptr<Foo> f) : f_(f) {}
};

class Baz {
     Handle<Foo> f_;
   public:
     Baz(Handle<Foo> f) : f_(f) {}
};


boost::shared_ptr<Foo> f1(new Foo); // points to a Foo object X
Bar bar(f1); // bar.f_ points to the same Foo object Y

f1 = boost::shared_ptr<Foo>(new Foo);
// after the above, f1 points to a new Foo object Y,
// while bar.f_ still points to X.

Handle<Foo> f2(boost::shared_ptr<Foo>(new Foo));  // points to a Foo Z
Baz baz(f2); // baz.f_ points to Z too

f2.linkTo(boost::shared_ptr<Foo>(new Foo));
// after the above, f2 points to a new Foo object W,
// and baz.f_ points to the new W as well.


This allows one, for instance, to instantiate a number of bonds passing  
them copies of a Handle to a yield term structure. Later, linking the  
handle to another term structure causes all bonds to see the change and  
recalculate their price accordingly.

Later,
        Luigi

----------------------------------------

The purpose of abstraction is not to be vague, but to create a new
semantic level in which one can be absolutely precise.
-- W.E. Dijkstra