Smart pointer implementation

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

John Harrison said:
If you intend to put those object into a vector, then you are going to find
it hard to avoid them being referred to more than once.

Well, unless of course I do it the stupid way I'm doing now ;) See,
with my way, I get
v[0] is a
v[1] is c
v[2] is c
v[3] is d

v[0] is a
v[1] is copy of c
v[2] is c
v[3] is d

(and so on)

It works, but I now see that it's a bad way to do it. Curse Borland
for making me have to do this in the first place ;(

I appreciate your help :)
 
C

Christopher Benson-Manica

Karl Heinz Buchegger said:
I just read in another posting that you want to put those pointers into a
standard container. That will not work with the above.

Doesn't it...? My current, perhaps bogus, understanding is that my
way works, just very inefficiently:

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

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

So instead of trading references to the same object (which I now see
is the Good Way to do this), the object always gets copied. So
nothing ever gets left behind, but there are also a bunch of
unnecessary memory operations. Well, at least that's how it works in
my fantasy world...
 
K

Karl Heinz Buchegger

Christopher said:
Doesn't it...?

The comment of not working has to be seen in the light of
hiding the copy constructor and assignment operator.
The standard container request your objects to be able
to perform those operations.
No copy constructor, no assignment operator -> no use in
a standard container.
 
C

Christopher Benson-Manica

Karl Heinz Buchegger said:
No copy constructor, no assignment operator -> no use in
a standard container.

Well, the StupidPointer has both, right? Does it work with simple
parameters (such as int)? Or am I a StupidProgrammer? ;)
 
K

Karl Heinz Buchegger

Christopher said:
Well, the StupidPointer has both, right? Does it work with simple
parameters (such as int)? Or am I a StupidProgrammer? ;)

It is fine.

The order of events was:
* You said, that you plan to have only one pointer to the object at any time
* I suggested, in order to achieve this, to 'hide' the cctor and the op= by
making them private and not defining them. By doing this you could
*ensure* that only one pointer ever existed to the object.
To be able to ensure something is definitly better then to depend on
the programmers memory to not do it.
* Then it turned out that you want to build collections from this pointer
* In order to do this, the cctor and op= cannot be private, otherwise
you would not be able to use the containers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top