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 7658539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:12:54+00:00 2026-05-31T13:12:54+00:00

I need to find a solution to allow a subclass to get its proper

  • 0

I need to find a solution to allow a subclass to get its proper smart pointer.

class Parent : public enable_shared_from_this {
  ...
}

class Child : public Parent {
  public Child(){
    boost::shared_ptr<Parent> pointer=shared_from_this(); // should work
    boost::shared_ptr<Child> pointer=shared_from_this(); // won't work.

  ...
}

How do I get the right smart pointer using shared_from_this()?

CONTEXT:

I’m writing a bit of notifier/listener stuff, and some classes will naturally need to register and unregister themselves from the notifier. For example,

class Body : extends Listener<BodyMessage>{ // listen for BodyMessage messages
  public:
    Body() {
      Notifier<BodyMessage>::register(this); // register with the appropriate notifier
    }

    virtual ~Body {
      Notifier<BodyMessage>::unregister(this); // unregister
    }

    bool notify(BodyMessage m){ ... }

  ...
}

Normally I would just use the this pointer, and all would be well. I’ve gotten the Notifier to use templates, so I can pass messages only to the ones that want to hear them.

However, I want to use smart pointers. If the notifier looks like this:

template<typename t>
class Notifier {
  public:
    static void register<boost::shared_ptr<Listener<t>>> boost::shared_ptr<Listener<t>> listener);

  ...
}

then I cannot use the this pointer any more. Naturally, I made Body extend enable_shared_from_this:

class Body : public boost::enable_shared_from_this, public Listener<BodyMessage> {
  public:
    Notifier<BodyMessage>::register(get_shared_ptr());
  ...
}

And that seems to works for Bodies. It doesn’t work, however, for subclasses of bodies (or, at least, it doesn’t seem to):

class BodyChild : public Body {
  public:
    BodyChild(){
      Notifier<BodyMessage>::register(get_shared_ptr());
}

likely because I can’t cast a shared_pointer. SO, can I make a solution that

  • lets me use shared pointers for the listeners (since these listeners are also used in other smart pointer contexts),
  • lets me template the Notifier and the listeners, using the message type itself for the template, so it’s super easy to listen for the specific messages and so I don’t have to decode a message, and
  • is simple?

I’m open to other ideas, but if I can get this to work, I’ll be thrilled.

  • 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-05-31T13:12:55+00:00Added an answer on May 31, 2026 at 1:12 pm

    You can cast smart pointers, and Boost provides you with a few templates to ease this. You have eg. static_pointer_cast and dynamic_pointer_cast which allow you to cast “through” the pointer.

    Since this is of the right dynamic type, you can invoke boost::static_pointer_cast on the return value of shared_from_this():

    boost::shared_ptr<Child> p = static_pointer_cast<Child>(shared_from_this());
    

    (no need to qualify static_pointer_cast thanks to Koenig lookup)

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

Sidebar

Related Questions

I already googled around to find a solution for my need, with no success.
i have a big solution containing that much projects..i need to find a specific
I often find myself in need of writing down a future Visual Studio solution
I'm attempting to find a solution to allow users to sort content that is
I've been working on this for days and I can't find a proper solution,
I need to find out how to format numbers as strings. My code is
I need to find a bottleneck and need to accurately as possible measure time.
I need to find the PID of the current running process on a Linux
I need to find/create an application that will create employee web usage reports from
I need to find occurrences of (+) in my sql scripts, (i.e., Oracle outer

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.