Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8687329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:05:35+00:00 2026-06-12T23:05:35+00:00

I have a base class which derives from boost::enable_shared_from_this, and then another class which

  • 0

I have a base class which derives from boost::enable_shared_from_this, and then another class which derives from both the base class and boost::enable_shared_from_this:

#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>

using namespace boost;

class A : public enable_shared_from_this<A> { };

class B : public A , public enable_shared_from_this<B> {
public:
    using enable_shared_from_this<B>::shared_from_this;
};

int main() {
shared_ptr<B> b = shared_ptr<B>(new B());
shared_ptr<B> b_ = b->shared_from_this();

return 0;
}

This compiles but at runtime it gives

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> >'
  what():  tr1::bad_weak_ptr
Aborted

What is causing this, and is there some way around it?

EDIT:

What if I need something like this:

class A : public enable_shared_from_this<A> { };
class B : public enable_shared_from_this<B> { };    

class C : public A, public B, public enable_shared_from_this<C> {
public:
    using enable_shared_from_this<C>::shared_from_this;
};

such that A and B both need shared_from_this on their own (and one can’t inherit it from the other), and C needs A, B, and shared_from_this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:05:35+00:00Added an answer on June 12, 2026 at 11:05 pm

    Here’s how I would solve your problem:

    #include <boost/enable_shared_from_this.hpp>
    #include <boost/shared_ptr.hpp>
    
    using namespace boost;
    
    class virt_enable_shared_from_this :
       public enable_shared_from_this<virt_enable_shared_from_this>
    {
     public:
       virtual ~virt_enable_shared_from_this() {}
    };
    
    template <class T>
    class my_enable_shared_from_this : virtual public virt_enable_shared_from_this
    {
     public:
       shared_ptr<T> shared_from_this() {
          return dynamic_pointer_cast<T>(virt_enable_shared_from_this::shared_from_this());
       }
    };
    
    class A : public my_enable_shared_from_this<A> { };
    
    class B : public my_enable_shared_from_this<B> { };
    
    class C : public A, public B, public my_enable_shared_from_this<C> {
     public:
       using my_enable_shared_from_this<C>::shared_from_this;
    };
    
    int main() {
       shared_ptr<C> c = shared_ptr<C>(new C());
       shared_ptr<C> c_ = c->shared_from_this();
    
       return 0;
    }
    

    This is painful and at least a bit ugly. But it works reasonably well, after a fashion. I think Fraser‘s idea of re-thinking your design is likely the better option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class, A , in C++/CLI which derives from a templated base
I have a class which is derived from a base class, and have many
I have written a base class from which I wish to derive several child
I have 2 web user controls, both inherit the same base class which extends
As part of a project I have a class which derives from NetworkStream. In
I have a class which derives from a QWidget and a model class. Based
In my current code I have an TNCPConnection class which is derived from enable_shared_from_this
I have a base and derived class called Node which form elements of a
I want to have a base class which dictates the alignment of the objects
Ok, so I have a base class which declares the event StatusTextChanged . My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.