diff --git a/eflxx/include/eflxx/CountedPtr.h b/eflxx/include/eflxx/CountedPtr.h index 0de851a..92ebab1 100644 --- a/eflxx/include/eflxx/CountedPtr.h +++ b/eflxx/include/eflxx/CountedPtr.h @@ -21,6 +21,8 @@ class CountedPtr { long* count; // shared number of owners public: + template friend class CountedPtr; + // initialize pointer with existing pointer // - requires that the pointer p is a return value of new explicit CountedPtr (T* p=0) @@ -29,20 +31,43 @@ class CountedPtr { } // copy pointer (one more owner) - CountedPtr (const CountedPtr& p) throw() + CountedPtr (const CountedPtr& p) : ptr(p.ptr), count(p.count) { ++*count; } - + + template + CountedPtr (const CountedPtr& p) + : ptr(p.ptr), count(p.count) + { + ++*count; + } + + template + static CountedPtr cast_static (const CountedPtr& p) + { + T *obj = static_cast (&(*p)); + + return CountedPtr (obj); + } + + template + static CountedPtr cast_dynamic (const CountedPtr& p) + { + T *obj = dynamic_cast (&(*p)); + + return CountedPtr (obj); + } + // destructor (delete value if this was the last owner) - ~CountedPtr () throw() + ~CountedPtr () { dispose(); } // assignment (unshare old and share new value) - CountedPtr& operator= (const CountedPtr& p) throw() + CountedPtr& operator= (const CountedPtr& p) { if (this != &p) { dispose(); @@ -54,11 +79,11 @@ class CountedPtr { } // access the value to which the pointer refers - T& operator*() const throw() + T& operator*() const { return *ptr; } - T* operator->() const throw() + T* operator->() const { return ptr; }