Thomas J. Gritzan wrote:
Protoman schrieb:
OK, here's the new SmrtPtr class:
// COPYRIGHT CMDR DOUGLAS I. PEREIRA 07/10/06
// ALL UNAUTHORIZED THIRD PARTY USE IS PROHIBITED
// I HAVE WORKED VERY HARD AND HAVE SPENT HOURS OF
// TIME DEVELOPING THIS. DO NOT COPY IT OR I WILL SUE YOU
// ************************************************************
#pragma once
#include <iostream>
#include "SmrtPtrDB.hpp"
using namespace std;
class NullPtr{};
template<class T>
class SmrtPtr
{
public:
explicit SmrtPtr<T>(T* obj=0):ptr(obj), DataBase(new SmrtPtrDB){}
SmrtPtr<T>(const SmrtPtr<T>& rhs):ptr(rhs.ptr), DataBase(new
SmrtPtrDB(rhs.DataBase->status())){DataBase->add();}
The smartpointers should _share_ a common SmrtPtrDB, if they share a
common object pointer. Here you create another copy of SmrtPtrDB in the
copy constructor.
~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{delete ptr;cout << "Deleted." << endl;}
else cout << "Out of scope. " << endl;
}
You don't delete the SmrtPtrDB.
void operator=(T val){if(ptr==0)throw NullPtr();else *ptr=val;}
Remove this. The user should dereference the smart pointer to access its
value.
void operator=(T* val){ptr=val;}
Here you should release the old ptr/SmrtPtrDB and create a new pair.
Something like this:
void operator=(T* val)
{
SmrtPtr<T> temp(val);
swap(temp);
}
SmrtPtr<T>& operator=(const SmrtPtr<T>& rhs)
{
if(this!=&rhs)
{
SmrtPtr<T> temp(rhs);
swap(temp);
}
else return *this;
}
You return *this in the "else" but not in the "if". Why?
bool operator==(const SmrtPtr<T>& rhs)const{if(ptr==rhs.ptr)return
true;else return false;}
bool operator!=(const SmrtPtr<T>& rhs)const{if(ptr!=rhs.ptr)return
true;else return false;}
bool operator<=(const SmrtPtr<T>& rhs)const{if(ptr<=rhs.ptr)return
true;else return false;}
bool operator>=(const SmrtPtr<T>& rhs)const{if(ptr>=rhs.ptr)return
true;else return false;}
int status(){return DataBase->status();}
T& operator*()const{if(ptr==0)throw NullPtr();else return *ptr;}
T* operator->()const{if(ptr==0)throw NullPtr();else return ptr;}
operator T*()const{if(ptr==0)throw NullPtr();else return ptr;}
private:
void swap(SmrtPtr<T>& rhs){this=&rhs;}
Better this way:
void swap(SmrtPtr<T>& rhs)
{
std::swap(ptr,rhs.ptr);
std::swap(DataBase,rhs.DataBase);
}
mutable SmrtPtrDB* DataBase;
T* ptr;
};
Well, your operator= should return *this by reference, as every
operator= does.
Anyway, if you won't get your indentation right, I won't read your code
anymore. It is horrible...
--
Thomas
cout << "Out of scope. " << endl;