Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > smart pointers

Reply
Thread Tools

smart pointers

 
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      02-14-2008

When I am using the private implementation idiom, is there anything to
be gained by using a boost::scoped_ptr as opposed to a auto_ptr? I
confess I'm not that clear on the differences...
 
Reply With Quote
 
 
 
 
Cholo Lennon
Guest
Posts: n/a
 
      02-14-2008
On Feb 14, 10:27 am, (E-Mail Removed) wrote:
> When I am using the private implementation idiom, is there anything to
> be gained by using a boost::scoped_ptr as opposed to a auto_ptr? I
> confess I'm not that clear on the differences...


If your classes are noncopyable: a const auto_ptr to hold the pimpl is
almost the same than scoped_ptr: scoped_ptr can be 'reset' to change
the pointee pimpl. You can't change the pointee pimpl with const
auto_ptr

If your classes are copyable: auto_ptr can't be const and non const
auto_ptr leads you to the disaster due to auto_ptr's transfer
ownership. When my classes are copyable I prefer using shared_ptr to
hold the pimpl (to avoid the manual coding of pimpl copy)

Regards

--
Cholo Lennon
Bs.As.
ARG
 
Reply With Quote
 
 
 
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      02-14-2008
On 14 Feb, 13:57, Cholo Lennon <(E-Mail Removed)> wrote:
> On Feb 14, 10:27 am, (E-Mail Removed) wrote:
>
> > When I am using the private implementation idiom, is there anything to
> > be gained by using a boost::scoped_ptr as opposed to a auto_ptr? I
> > confess I'm not that clear on the differences...

>
> If your classes are noncopyable: a const auto_ptr to hold the pimpl is
> almost the same than scoped_ptr: scoped_ptr can be 'reset' to change
> the pointee pimpl. You can't change the pointee pimpl with const
> auto_ptr
>
> If your classes are copyable: auto_ptr can't be const and non const
> auto_ptr leads you to the disaster due to auto_ptr's transfer
> ownership. When my classes are copyable I prefer using shared_ptr to
> hold the pimpl (to avoid the manual coding of pimpl copy)


I'm unclear exactly what non-copyable means in this context. The
following seems to copy just fine.

Here's part of a wrapper class for an MD5 hash I've been playing with
to get to grips with this pimpl stuff:

//-----------------------------------
#include <boost/scoped_ptr.hpp>
#include <stdint.h>
#include "hash.h"

class MD5Private;
class MD5 : public virtual Hash
{
public:
using Hash::hash;
using Hash::end;
using Hash::digest;

MD5();
MD5(const MD5& h);
MD5& operator= (const MD5& h) throw();
virtual ~MD5() throw();

virtual void begin () throw();
virtual void hash (const uint8_t* data, const uint_32t& len)
throw();
virtual void end (const uint8_t* hval) throw();

private:
boost::scoped_ptr<MD5Private> p_;
};
//--------------------------

and in the relevant bits of the class definition I currently have:

//--------------------------
class MD5Private {
public:
uint32_t i[2];
uint32_t buf[4];
uint8_t in[64];
uint8_t digest[16];
};

// New object, so initialise
MD5::MD5() : p_(new MD5Private()) { begin(); }

// Copy object, so do _not_ initialise
MD5::MD5(const MD5& other) :
Hash(other),
p_(other.p_.get() ? new MD5Private(*other.p_) : NULL)
{ }

MD5::~MD5() throw() { }

MD5&
MD5:perator= (const MD5& rhs) throw()
{
MD5 temp(rhs);
swap(p_, temp.p_);
return *this;
}
//<etc, etc>
//--------------------------

Are you saying that by using a boost::scoped_ptr this should not work?
 
Reply With Quote
 
Cholo Lennon
Guest
Posts: n/a
 
      02-14-2008
On Feb 14, 1:35 pm, (E-Mail Removed) wrote:
> On 14 Feb, 13:57, Cholo Lennon <(E-Mail Removed)> wrote:
>
> > On Feb 14, 10:27 am, (E-Mail Removed) wrote:

>
> > > When I am using the private implementation idiom, is there anything to
> > > be gained by using a boost::scoped_ptr as opposed to a auto_ptr? I
> > > confess I'm not that clear on the differences...

>
> > If your classes are noncopyable: a const auto_ptr to hold the pimpl is
> > almost the same than scoped_ptr: scoped_ptr can be 'reset' to change
> > the pointee pimpl. You can't change the pointee pimpl with const
> > auto_ptr

>
> > If your classes are copyable: auto_ptr can't be const and non const
> > auto_ptr leads you to the disaster due to auto_ptr's transfer
> > ownership. When my classes are copyable I prefer using shared_ptr to
> > hold the pimpl (to avoid the manual coding of pimpl copy)

>
> I'm unclear exactly what non-copyable means in this context. The
> following seems to copy just fine.
>
> Here's part of a wrapper class for an MD5 hash I've been playing with
> to get to grips with this pimpl stuff:
>
> //-----------------------------------
> #include <boost/scoped_ptr.hpp>
> #include <stdint.h>
> #include "hash.h"
>
> class MD5Private;
> class MD5 : public virtual Hash
> {
> public:
> using Hash::hash;
> using Hash::end;
> using Hash::digest;
>
> MD5();
> MD5(const MD5& h);
> MD5& operator= (const MD5& h) throw();
> virtual ~MD5() throw();
>
> virtual void begin () throw();
> virtual void hash (const uint8_t* data, const uint_32t& len)
> throw();
> virtual void end (const uint8_t* hval) throw();
>
> private:
> boost::scoped_ptr<MD5Private> p_;};
>
> //--------------------------
>
> and in the relevant bits of the class definition I currently have:
>
> //--------------------------
> class MD5Private {
> public:
> uint32_t i[2];
> uint32_t buf[4];
> uint8_t in[64];
> uint8_t digest[16];
>
> };
>
> // New object, so initialise
> MD5::MD5() : p_(new MD5Private()) { begin(); }
>
> // Copy object, so do _not_ initialise
> MD5::MD5(const MD5& other) :
> Hash(other),
> p_(other.p_.get() ? new MD5Private(*other.p_) : NULL)
> { }
>
> MD5::~MD5() throw() { }
>
> MD5&
> MD5:perator= (const MD5& rhs) throw()
> {
> MD5 temp(rhs);
> swap(p_, temp.p_);
> return *this;}
>
> //<etc, etc>
> //--------------------------
>
> Are you saying that by using a boost::scoped_ptr this should not work?


No, I'm saying that using scoped_ptr you have to define copy ctor and
operator= like you did. Your code seems to be perfectly valid.

and... I'm sorry, I did a mistake when I said:

"When my classes are copyable I prefer using shared_ptr to
hold the pimpl (to avoid the manual coding of pimpl copy)"

It should have been:

"When my classes are copyable and the internals details have to be
shared I use shared_ptr (with shared_ptr you don't have to define copy
ctor and operator="

(It's very common for me to use classes with internal shared details,
this was the source of my mistake).


BTW, I prefer using a nested private class for pimpl data:

class MD5 {

private:
class Private;
boost::scoped_ptr<Private> p_;
};

....

class MD5:rivate { ... };



Regards


--
Cholo Lennon
Bs.As.
ARG
 
Reply With Quote
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      02-15-2008
On 14 Feb, 16:00, Cholo Lennon <(E-Mail Removed)> wrote:
> On Feb 14, 1:35 pm, (E-Mail Removed) wrote:
>
> > Are you saying that by using a boost::scoped_ptr this should not work?

>
> No, I'm saying that using scoped_ptr you have to define copy ctor and
> operator= like you did. Your code seems to be perfectly valid.


Thanks. I think I've got it now.

> BTW, I prefer using a nested private class for pimpl data:
>
> class MD5 {
>
> private:
> class Private;
> boost::scoped_ptr<Private> p_;
>
> };
>
> ...
>
> class MD5:rivate { ... };


That's neater - I like it. Thanks again.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Smart Pointers: Is there something similar to smart pointers in C? MotoK C Programming 59 09-15-2006 07:03 PM
Smart pointers and member function pointers n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM
Smart Card Certificate Logon and Smart Card Wireless EAP-TLS erha Wireless Networking 0 05-19-2005 01:40 AM
trade 64mb smart media for 16mb smart media cards wjva Digital Photography 1 08-20-2003 08:30 PM



Advertisments