|
Boost Users : |
From: Johan Nilsson (r.johan.nilsson_at_[hidden])
Date: 2006-10-19 09:45:53
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);
BOOST_REQUIRE_EQUAL(1, pB.use_count());
{
shared_ptr<A> pA = pB->FooA();
BOOST_REQUIRE_EQUAL(2, pB.use_count());
BOOST_REQUIRE_EQUAL(pB.use_count(), pA.use_count());
}
BOOST_REQUIRE_EQUAL(1, pB.use_count());
}
-------------
// Johan
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