Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-10-19 12:26:42


Johan Nilsson wrote:
> Russell Hind wrote:
>> I thought each time you inherit from enable_shared_from_this<T> you
>> get a new reference count, so with the code below, won't you end up
>> with 2 separate reference counts to the same instance? I thought
>> this was a limitation of shared_from_this.
>
> The following passes for me (RC_1_34_0, vc8):
>
> -------
> #include <boost/test/auto_unit_test.hpp>
> #include <boost/enable_shared_from_this.hpp>
>
> using boost::shared_ptr;
> using boost::enable_shared_from_this;
>
> struct A : enable_shared_from_this<A>
> {};
>
> struct B : A, enable_shared_from_this<B>
> {
> shared_ptr<B> FooB()
> {
> return enable_shared_from_this<B>::shared_from_this();
> }
>
> shared_ptr<A> FooA()
> {
> return enable_shared_from_this<A>::shared_from_this();
> }
> };
>
> BOOST_AUTO_TEST_CASE(EnableSharedFromThisTest)
> {
> shared_ptr<B> pB(new B);

I have no idea why this compiles. It shouldn't, B has two
enable_shared_from_this<> bases, and the constructor will only initialize
one of them. Only one of FooA and FooB works (FooA in this case). I'll
investigate.


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