Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Smart pointer implementation

Reply
Thread Tools

Smart pointer implementation

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      11-16-2004
Is there anything wrong with my attempt (below) at implementing
something resembling a smart pointer?

template < class T >
class SmartPointer
{
private:
T *t;

public:
SmartPointer() {t=new T();}
SmartPointer( const SmartPointer<T> &rhs ) {t=rhs.t;}
T& operator*() const {return(*t);}
T* operator->() const {return(t);}
SmartPointer<T>& operator= ( const SmartPointer<T> &rhs ) {t=rhs.t; return(*this);}
~SmartPointer() {delete t;}
};

The program I wrote to test this crashes, and I wouldn't be at all
surprised to learn that I've made a mistake here somewhere. And no, I
can't use Boost for this.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-16-2004
Christopher Benson-Manica wrote:
>
> Is there anything wrong with my attempt (below) at implementing
> something resembling a smart pointer?
>
> template < class T >
> class SmartPointer
> {
> private:
> T *t;
>
> public:
> SmartPointer() {t=new T();}
> SmartPointer( const SmartPointer<T> &rhs ) {t=rhs.t;}
> T& operator*() const {return(*t);}
> T* operator->() const {return(t);}
> SmartPointer<T>& operator= ( const SmartPointer<T> &rhs ) {t=rhs.t; return(*this);}
> ~SmartPointer() {delete t;}
> };
>
> The program I wrote to test this crashes, and I wouldn't be at all
> surprised to learn that I've made a mistake here somewhere. And no, I
> can't use Boost for this.


Yep. You copy constructor is wrong. It does a memberwise copy, when
it should do a deep copy.

SmartPointer( const SmartPointer<T> &rhs ) {t = new T( rhs.t ); }

same for your assignment operator

--
Karl Heinz Buchegger

 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-16-2004
Karl Heinz Buchegger wrote:
>
> Christopher Benson-Manica wrote:
> >
> > Is there anything wrong with my attempt (below) at implementing
> > something resembling a smart pointer?
> >
> > template < class T >
> > class SmartPointer
> > {
> > private:
> > T *t;
> >
> > public:
> > SmartPointer() {t=new T();}
> > SmartPointer( const SmartPointer<T> &rhs ) {t=rhs.t;}
> > T& operator*() const {return(*t);}
> > T* operator->() const {return(t);}
> > SmartPointer<T>& operator= ( const SmartPointer<T> &rhs ) {t=rhs.t; return(*this);}
> > ~SmartPointer() {delete t;}
> > };
> >
> > The program I wrote to test this crashes, and I wouldn't be at all
> > surprised to learn that I've made a mistake here somewhere. And no, I
> > can't use Boost for this.

>
> Yep. You copy constructor is wrong. It does a memberwise copy, when
> it should do a deep copy.
>
> SmartPointer( const SmartPointer<T> &rhs ) {t = new T( rhs.t ); }
>
> same for your assignment operator


Actually the situation for the assignment operator is worse.
As it is now, it additionally leaks memory

--
Karl Heinz Buchegger

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      11-16-2004
Karl Heinz Buchegger <> spoke thus:

> Yep. You copy constructor is wrong. It does a memberwise copy, when
> it should do a deep copy.


Right, right... d'oh...

> SmartPointer( const SmartPointer<T> &rhs ) {t = new T( rhs.t ); }


With delete t; coming before the assignment, of course Thanks!

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-16-2004

"Christopher Benson-Manica" <> wrote in message
news:cnd76m$sni$...
> Is there anything wrong with my attempt (below) at implementing
> something resembling a smart pointer?
>
> template < class T >
> class SmartPointer
> {
> private:
> T *t;
>
> public:
> SmartPointer() {t=new T();}
> SmartPointer( const SmartPointer<T> &rhs ) {t=rhs.t;}
> T& operator*() const {return(*t);}
> T* operator->() const {return(t);}
> SmartPointer<T>& operator= ( const SmartPointer<T> &rhs ) {t=rhs.t;
> return(*this);}
> ~SmartPointer() {delete t;}
> };
>
> The program I wrote to test this crashes, and I wouldn't be at all
> surprised to learn that I've made a mistake here somewhere. And no, I
> can't use Boost for this.
>


Your smart pointer is not very smart (sorry). It is basically no different
from a raw pointer. When you copy, you end up with two pointers to the same
object and no idea when it is safe to delete the pointer.

The commonest implementation of a smart pointer uses reference counting to
overcome this problem. The reference count counts how many copies of the
smart pointer you have. When the count reaches zero you know it is safe to
delete the pointer. Reference counting comes in two varieties, intrusive and
non-intrusive. With intrusive reference counting the count is held in the
pointed-to object itself, with non-intrusive the reference count is help
outside the pointed-to object.

Have a look on the web for reference counted smart pointer implementations.
Then have a look at boost, when they have high quality implementations of
both types (but a bit more complex than your typical implementation).

John


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-16-2004
Christopher Benson-Manica wrote:
>
> Karl Heinz Buchegger <> spoke thus:
>
> > Yep. You copy constructor is wrong. It does a memberwise copy, when
> > it should do a deep copy.

>
> Right, right... d'oh...
>
> > SmartPointer( const SmartPointer<T> &rhs ) {t = new T( rhs.t ); }

>
> With delete t; coming before the assignment, of course Thanks!
>


Definitly not.
This is a constructor. t has no meaningful value.
Actually the whole thing should be written as

SmartPointer( const SmartPointer<T> &rhs ) : t( new T( rhs.t ) ) {}

--
Karl Heinz Buchegger

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-16-2004

"Karl Heinz Buchegger" <> wrote in message
news:...
> Christopher Benson-Manica wrote:
>>
>> Is there anything wrong with my attempt (below) at implementing
>> something resembling a smart pointer?
>>
>> template < class T >
>> class SmartPointer
>> {
>> private:
>> T *t;
>>
>> public:
>> SmartPointer() {t=new T();}
>> SmartPointer( const SmartPointer<T> &rhs ) {t=rhs.t;}
>> T& operator*() const {return(*t);}
>> T* operator->() const {return(t);}
>> SmartPointer<T>& operator= ( const SmartPointer<T> &rhs ) {t=rhs.t;
>> return(*this);}
>> ~SmartPointer() {delete t;}
>> };
>>
>> The program I wrote to test this crashes, and I wouldn't be at all
>> surprised to learn that I've made a mistake here somewhere. And no, I
>> can't use Boost for this.

>
> Yep. You copy constructor is wrong. It does a memberwise copy, when
> it should do a deep copy.
>
> SmartPointer( const SmartPointer<T> &rhs ) {t = new T( rhs.t ); }
>
> same for your assignment operator
>


A smart pointer implemented like that doesn't have many uses. Most of the
time I would prefer to simply create and copy objects than use such a smart
pointer.

john


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-16-2004
John Harrison wrote:
>

[snip]
>
> A smart pointer implemented like that doesn't have many uses. Most of the
> time I would prefer to simply create and copy objects than use such a smart
> pointer.


Agreed.
But if it makes the OP happy


--
Karl Heinz Buchegger

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      11-16-2004
Karl Heinz Buchegger <> spoke thus:

> This is a constructor. t has no meaningful value.


I realized that after I posted...

> Actually the whole thing should be written as


> SmartPointer( const SmartPointer<T> &rhs ) : t( new T( rhs.t ) ) {}


That'd be nice, except that T doesn't have a copy constructor ;(

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      11-16-2004
Karl Heinz Buchegger <> spoke thus:

> But if it makes the OP happy


Well, the whole exercise is necessary because I want to put some
objects in standard containers, but they don't fit in the containers
directly (thanks to their use of stupid Borland extensions).
Therefore I figured this would be the easiest way to put them in a
standard container...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
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
Never ever use a raw pointer when a smart pointer can do the same job Hicham Mouline C++ 100 08-25-2009 05:07 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 1 09-06-2006 03:00 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 3 09-06-2006 02:58 PM
Smart pointer implementation. maadhuu C++ 3 12-03-2005 05:05 PM
Critique requested for a smart pointer implementation Asfand Yar Qazi C++ 2 10-20-2003 11:45 PM



Advertisments