It is (almost*) never a good idea to mix business logic with life cycle management.
For this reason shared ownership with reference counting in C++11 standard library
and well known Boost library
is implemented in separate template classes encapsulating most commonly required ownership schemes.
In Boost library
these are:
shared_ptr<T>
- shared ownership with reference counting (reference counter is allocated when the first instance ofshared_ptr<T>
for particular raw pointerT*
is created).weak_ptr<T>
- a handle that can be used to obtain fullshared_ptr<T>
if that one is still alive*
intrusive_ptr<T>
- shared ownership with reference counting where reference counter is a part of the object being managed. This particular class is an example of what you are trying to achieve, but already polished to industrial standard.