Boost logo

Boost :

From: Daniel Wallin (dalwan01_at_[hidden])
Date: 2004-06-25 20:43:22


Stefan Slapeta wrote:
> class intrusive_ptr_base
> {
> friend void intrusive_ptr_add_ref(intrusive_ptr_base*);
> friend void intrusive_ptr_release(intrusive_ptr_base*);
>
> int ref_count__;
>
> protected:
> intrusive_ptr_base() : ref_count__(0) {}
>
> virtual ~intrusive_ptr_base() {}

I don't necessarily think this is a good idea, but I will only comment
on the code. You can use CRTP here instead, to avoid the virtual
destructor. Something like:

   template<class Derived>
   class intrusive_ptr_base
   {
       friend void intrusive_ptr_add_ref(intrusive_ptr_base* o)
       {
           ++o->ref_count__;
       }

       friend void intrusive_ptr_release(intrusive_ptr_base* o)
       {
           if (--o->ref_count__ == 0) {
               delete static_cast<Derived*>(o);
           }
       }

       int ref_count__;

   protected:
       intrusive_ptr_base() : ref_count__(0) {}
   };

-- 
Daniel Wallin

Boost list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk