Boost logo

Boost :

From: Ulrich Eckhardt (doomster_at_[hidden])
Date: 2007-10-07 05:34:49


On Saturday 06 October 2007 23:10:52 Mathias Gaunard wrote:
> Vladimir Batov wrote:
> > shared_ptr is an extremely versatile and poweful gadget. One (among
> > others) thing it handles well is incomplete classes for implementation
> > hiding. Maybe there are other smart pointers doing that sort of thing
> > cheaper. I am not sure.
>
> Here is a simple deep-copying smart pointer.

Where from? I hope that one is not in productive use, because...

> template<typename T>
> struct copy_ptr
> {
[...]
> explicit copy_ptr(T* ptr_) : ptr(ptr_)
> {
> }

...this should use std::auto_ptr to make clear that ownership is transferred
and...

> copy_ptr(const copy_ptr& p) ptr(p.ptr ? new(ptr) T(*p.ptr) : 0)
> {
> }
>
> copy_ptr& operator=(const copy_ptr& p)
> {
> if(ptr)
> {
> if(p.ptr)
> {
> *ptr = *p.ptr;
> }
> else
> {
> delete ptr;
> ptr = 0;
> }
> }
> else if(p.ptr)
> {
> new(ptr) T(*p.ptr);
> }
> }

...it looks like you are using placement new on a null pointer in the
assignment and on an uninitialised pointer in the copy constructor, or am I
missing something?

> void swap(copy_ptr& p)
> {
> T* tmp = ptr;
> ptr = p.ptr;
> p.ptr = ptr;
> }

How about
  std::swap( p.ptr, ptr);
?

> T* operator->()
> {
> return ptr;
> }
>
> const T* operator->() const
> {
> return ptr;
> }

Why? I mean since when is the CV qualification of a pointer relevant for the
CV qualification of the pointee?

Uli


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk