Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2006-08-24 11:31:32


"Ovanes Markarian" <om_boost_at_[hidden]> writes:

> I have a question regarding shared_ptr implementation:
>
>
> currently shared_ptr is implementated so, that it contains:
>
> T * px; // contained pointer
> detail::shared_count pn; // reference counter
>
>
> I am interested to know philosophical thoughts why this kind of decision was made to implement the
> shared_ptr class this way?
>
> I assumed to save space which is consumed by such a pointer one could move T pointer (T*) to
> shared_count class. All shared_ptr classes would contain only one instance or (may be better
> pointer to counter):
>
> detail::shared_count* pn; // reference counter

That scheme doesn't handle derived-to-base pointer conversions. You
can't make this work unless you have a separate pointer to the base
object:

  struct A { int x; virtual ~A(){} };
  struct B { int x; virtual ~B(){} };
  struct C : A,B {};
  shared_ptr<C> c(new C);
  shared_ptr<A> a(c);
  shared_ptr<B> b(c);

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net