template<class T>
class Handle
{
public:
friend class T2;
// constructor
explicit Handle(T* p) : ptr(p),
count(new long(1))
{
}
// copy constructor
Handle(const Handle<T>& p) throw() : ptr(p.ptr),
count(p.count)
{
++*count;
}
// destructor
~Handle() throw ()
{
release();
}
// assignment operator
Handle<T>& operator=(const Handle<T>& p) throw()
{
if(this != &p) // protect from self assignment
{
release();
ptr = p.ptr;
count = p.count;
++*count;
}
return *this;
}
// conversion operator, used for inheritance
template<class T2> operator Handle<T2>()
{
return Handle<T2>(p);
}
// return a reference via the deferencing operator
T& operator *() const throw()
{
return *ptr;
}
// return a pointer to the internal data
T* operator ->() const throw()
{
return ptr;
}
// assign a new representation
void Bind(T* p)
{
release();
ptr = p;
count = new long(1);
}
// return the reference count
long Count() const throw()
{
return *count;
}
private:
// decrememnt the count var and perform check to determine
// if internal data reps can be freed
void release()
{
if(--*count == 0)
{
delete count;
delete ptr;
// make sure pointers are NULL'd
count = 0;
ptr = 0;
}
}
// point to rep and reference count var
T *ptr;
long *count;
};
namespace std
{
using digitalfiends::Handle;
template<class T> ostream& operator <<(ostream& os, const Handle<T>& rhs)
{
return os << *rhs;
}
}
class Animal
{
public:
virtual void Speak() { cout << "Animal" << endl;
};
class Dog : public Animal
{
public:
void Speak() { cout << "Dog" << endl;
};
class Cat : public Animal
{
public:
void Speak() { cout << "Cat" << endl;
};
int main()
{
Handle<Animal> myanimal(new Animal);
Handle<Dog> mydog(new Dog);
myanimal = mydog; // error: no binary '='
return 0;
}
You can see by the conversion operator, operator Handle
, that this should work but alas, it doesn't.
The problem I have with this is that in Bjarne Stroustrup's, "The C++ Programming Language - Special Edition", on page 350 he shows how to make a 'smart point' that understands inheritance/polymorphism. Unfortunately it doesn't seem to work.
Anyone have any other ideas or approaches?
BTW I'm using Visual C++ 6.0 SP4.
Thanks in advance guys,
- Dire Wolf
direwolf@digitalfiends.com
Edited by - Dire.Wolf on 9/3/00 3:09:20 PM
Edited by - Dire.Wolf on 9/3/00 3:10:46 PM