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)ointee(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.
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)ointee(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.