Hello,
I'm trying to created base class and template for "smart" pointer
pointing to it. It should do simple reference counting and clean up when
the reference count reaches zero. The header looks like this:
class base_t {
int _ref_count;
public:
base_t(void): _ref_count(0) {};
}
template <typename T>
class ptr_t {
T *_target;
public:
ptr_t(void): _target(NULL) {};
ptr_t(T *);
ptr_t(ptr_t &);
virtual ~ptr_t(void);
T *operator->(void) {return _target;};
T &operator*(void) {return *_target;};
const T &operator*(void) const {return *_target;};
ptr_t &operator=(ptr_t &);
operator bool(void) const {return bool(_target);};
};
My intention is to create descendant classes like:
class child_t: public base_t
{
// ... members here
public:
// ... more members here
};
typedef ptr_t<base_t> child_p;
I have following questions:
1) Because base_t::_ref_count is private I have to make ptr_t it's
friend - but it is template. How can I specify that all classes created
with ptr_t<T> are friends of base_t?
2) Is there way to specify that type T in ptr_t<T> must be direct (or
indirect) descendant of base_t type?
Thank you all in advance.
Ales