Smart pointer implementation.

M

maadhuu

Hello,
I have tried this smart pointer implementation, but it is not working and
I am not able to figure out why .......Also, can you please suggest more
effective way/s of doing the same ???
Thank you,
Maadhuu.

//Smart.h

#ifndef _SMART_H
#define _SMART_H
#include<iostream>

using namespace std;

template<typename T>
struct PointerToT
{
explicit PointerToT<T>(T* realPtr=0):pointee(realPtr) {cout << "in
constructor";}
PointerToT(PointerToT& rhs)
{
pointee = rhs.pointee;
rhs.pointee = 0;
}
PointerToT<T>& operator=(PointerToT<T>& that)
{
if(this == &that) return *this;
delete pointee ;
pointee = that.pointee;
delete that.pointee ;
return *this;

}
~PointerToT<T>() { delete pointee;}

T& operator*() const { return *pointee;}
T* operator->() const { return &**this;} // &(this->operator*()) -
&(*this).operator*() -
//& (* *this);

private:
T* pointee;
};

#endif //_SMART_H

//smart.cpp

#include<iostream>
#include "smart.h"
using namespace std;

void printNode(ostream &os,const PointerToT<char>& ptr)
{
os << *ptr;
}
int main()
{
char *p = "abcdef";
cout << p;
PointerToT<char> ptr(p); //not working.

printNode(cout,ptr);

PointerToT<char> ptr1(ptr);
PointerToT<char> ptr2;
ptr2 = ptr1;

return 0;
}

Thank You once again.
 
M

Matthias Kaeppler

maadhuu said:
I have tried this smart pointer implementation, but it is not working and
I am not able to figure out why .......Also, can you please suggest more
effective way/s of doing the same ???

Sure: #include <boost/shared_ptr.hpp>

Time saving and effective! ;-)

Regards,
Matthias
 
N

Neelesh Bodas

maadhuu said:
Hello,
I have tried this smart pointer implementation, but it is not working and
I am not able to figure out why .......Also, can you please suggest more
effective way/s of doing the same ???

Donot reinvent the wheel. Look up auto_ptr. (They are not the "all
purpose" smart pointers, but they will suffice your needs as indicated
from this program)

Anyways, your program is working the way it is expected to work ;-)
 
J

Jonathan Mcdougall

maadhuu said:
Hello,
I have tried this smart pointer implementation, but it is not working and
I am not able to figure out why .......Also, can you please suggest more
effective way/s of doing the same ???
Thank you,
Maadhuu.

//Smart.h

#ifndef _SMART_H
#define _SMART_H
#include<iostream>

using namespace std;

Don't do that in a header.

http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5
template<typename T>
struct PointerToT
{
explicit PointerToT<T>(T* realPtr=0):pointee(realPtr) {cout << "in
constructor";}

Try to be careful with line breaks when posting.
PointerToT(PointerToT& rhs)
{
pointee = rhs.pointee;

Why not initialization?
rhs.pointee = 0;
}
PointerToT<T>& operator=(PointerToT<T>& that)
{
if(this == &that) return *this;
delete pointee ;
pointee = that.pointee;
delete that.pointee ;

Don't these two last lines ring a bell?
return *this;

}
~PointerToT<T>() { delete pointee;}

T& operator*() const { return *pointee;}
T* operator->() const { return &**this;} // &(this->operator*()) -
&(*this).operator*() -
//& (* *this);

What's all that? Check what you post!
private:
T* pointee;
};

#endif //_SMART_H

//smart.cpp

#include<iostream>
#include "smart.h"
using namespace std;

void printNode(ostream &os,const PointerToT<char>& ptr)
{
os << *ptr;
}
int main()
{
char *p = "abcdef";
cout << p;
PointerToT<char> ptr(p); //not working.

This should work ok.
printNode(cout,ptr);

PointerToT<char> ptr1(ptr);
PointerToT<char> ptr2;
ptr2 = ptr1;

return 0;

Now, assuming you corrected the operator= above, this will try to
delete something that was not allocated with new. This whole code is
equivalent to

delete "abcdef";

which is clearly not legal. Smart pointers are meant to be used with
dynamically allocated memory!
}

Thank You once again.

And what about std::auto_ptr?


Jonathan
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top