enable_shared_from_this & inheritance

Hello all I have one problem with enable_shared_from_this - i have one Base class, that inherit from enable_shared_from_this<Base>. But i also need to do shared_from_this from classes inherited from Base class. Without explicit inheritance from enable_shared_from_this for Derived code fail, but when i do enable_shared_from_this<Derived>, my code fail with "error: reference to 'shared_from_this' is ambiguous" How i can resolve this issue? -- With best wishes, Alex Ott http://alexott.blogspot.com/ http://content-filtering.blogspot.com/ http://xtalk.msk.su/~ott/

Alex Ott wrote:
Hello all
I have one problem with enable_shared_from_this - i have one Base class, that inherit from enable_shared_from_this<Base>. But i also need to do shared_from_this from classes inherited from Base class. Without explicit inheritance from enable_shared_from_this for Derived code fail, but when i do enable_shared_from_this<Derived>, my code fail with "error: reference to 'shared_from_this' is ambiguous" How i can resolve this issue?
Your best bet is probably to use dynamic_pointer_cast<Derived>( shared_from_this() ) in Derived.

Alex Ott wrote:
Hello all
I have one problem with enable_shared_from_this - i have one Base class, that inherit from enable_shared_from_this<Base>. But i also need to do shared_from_this from classes inherited from Base class. Without explicit inheritance from enable_shared_from_this for Derived code fail, but when i do enable_shared_from_this<Derived>, my code fail with "error: reference to 'shared_from_this' is ambiguous" How i can resolve this issue?
Do you mean something like this? --------- #include <boost/enable_shared_from_this.hpp> struct A : boost::enable_shared_from_this<A> {}; struct B : A , boost::enable_shared_from_this<B> { boost::shared_ptr<B> FooB() { return boost::enable_shared_from_this<B>::shared_from_this(); } boost::shared_ptr<A> FooA() { return boost::enable_shared_from_this<A>::shared_from_this(); } }; ------------ HTH // Johan

yes, thanks On 10/17/06, Johan Nilsson <r.johan.nilsson@gmail.com> wrote:
Alex Ott wrote:
Hello all
I have one problem with enable_shared_from_this - i have one Base class, that inherit from enable_shared_from_this<Base>. But i also need to do shared_from_this from classes inherited from Base class. Without explicit inheritance from enable_shared_from_this for Derived code fail, but when i do enable_shared_from_this<Derived>, my code fail with "error: reference to 'shared_from_this' is ambiguous" How i can resolve this issue?
Do you mean something like this?
---------
#include <boost/enable_shared_from_this.hpp>
struct A : boost::enable_shared_from_this<A> {};
struct B : A , boost::enable_shared_from_this<B> { boost::shared_ptr<B> FooB() { return boost::enable_shared_from_this<B>::shared_from_this(); }
boost::shared_ptr<A> FooA() { return boost::enable_shared_from_this<A>::shared_from_this(); } };
------------
HTH // Johan
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- With best wishes, Alex Ott, MBA http://alexott.blogspot.com/ http://content-filtering.blogspot.com/ http://xtalk.msk.su/~ott/

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. Russell Johan Nilsson wrote:
Do you mean something like this?
---------
#include <boost/enable_shared_from_this.hpp>
struct A : boost::enable_shared_from_this<A> {};
struct B : A , boost::enable_shared_from_this<B> { boost::shared_ptr<B> FooB() { return boost::enable_shared_from_this<B>::shared_from_this(); }
boost::shared_ptr<A> FooA() { return boost::enable_shared_from_this<A>::shared_from_this(); } };
------------
HTH // Johan

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

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.

Peter Dimov wrote:
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.
The code compiles on g++ and como, but neither FooA nor FooB works. Looks like an MSVC (and Borland) bug. It would be nice if I could find a way to make it fail at compile time instead of not working, though...
participants (4)
-
Alex Ott
-
Johan Nilsson
-
Peter Dimov
-
Russell Hind