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

  • SEARCH
  • Home
  • 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 3842680
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:49:42+00:00 2026-05-19T15:49:42+00:00

OK, I started using shared-pointers and pass shared-pointers as much as possible. No conversion

  • 0

OK, I started using shared-pointers and pass shared-pointers as much as possible. No conversion to raw pointers anymore. This works good, except in this specific case:

Suppose we have a class that also is an observer on another class, like this:

class MyClass : public IObserver
   {
   public:
      MyClass (std::shared_ptr<SomeOtherClass> otherClass);
      void DoSomethingImportant();
   private:
      std::shared_ptr<SomeOtherClass> m_otherClass;
   };

This class is used like this in my application:

std::shared_ptr<MyClass> myInstance(new MyClass(otherInstance));
...
myInstance->DoSomethingImportant();

MyClass gets a shared-pointer to another class and stores this in its m_otherClass data member.
In the DoSomethingImportant method, the MyClass instance does lots of important things, including registering itself as an observer on m_otherClass, like this:

m_otherClass->registerObserver(this);

The problem is that the registerObserver method is defined like this:

void registerObserver (std::shared_ptr observer);

It expects a shared pointer, but ‘this’ is a raw pointer, not a shared one.

I see three ways of solving this:

  • Find a trick to convert a normal pointer to a shared pointer (see question convert pointer to shared_ptr), but the answers to that question only suggest to copy the shared-pointers, not on how to actually convert the pointer to a shared pointer.
  • Pass the shared-pointer to ourself to the method, like this: “myInstance->DoSomethingImportant(myInstance);” which seems a bit stupid.
  • Put the observer part into a separate class. This looks like some overkill, and might make the class harder to understand.

This problem makes it obvious that shared-pointers are just an add-on to C++ (I don’t think you have the same problem in other languages/environments like C# (or .Net in general) and Java).

Any other suggestions or tricks on how to handle this situation?

  • 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-19T15:49:43+00:00Added an answer on May 19, 2026 at 3:49 pm

    What you need is probably the enable_shared_from_this and shared_from_this facilities. The docs are here

    Note that you cannot use shared_from_this until the constructor has fully completed and the object is already owned by another shared_ptr.

    struct test : boost::enabled_shared_from_this<test>
    {
        test() {
           // shared_from_this(this); // error, still not owned by another shared_ptr
        }
        boost::shared_ptr<test> shared() {
           return shared_from_this(this);
        }
    };
    int main() {
       test * t = new test;
       // boost::shared_ptr<test> p = t->shared(); // error, not yet owned by other shared_ptr
       boost::shared_ptr<test> owner( t ); 
       boost::shared_ptr<test> p = t->shared();     // [*] ok, "owner" owns the object
    }
    

    [*] This part of the example is silly, you could just copy owner into p, instead of calling the method. It is just presented to note when it is ok or not to called shared_from_this inside the test methods.

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

Sidebar

Related Questions

I just recently started using this library (the one from CodePlex), but I ran
I have recently started using a shared-host for my clients so see project progress
I just started using java so sorry if this question's answer is obvious. I
I have a shared hosting account, and I just started using source control locally
I have very recently started using a Bzr shared repository to manage my project.
I'm using this tutorial: http://blog.springsource.org/2011/01/04/green-beans-getting-started-with-spring-mvc/ to run a Spring MVC app using spring's tool
So I started using Git together with this workflow to make a personal website
This question is also started from following link: shared memory optimization confusion In above
Could anyone share some links on how to get started using QPID? I have
Started using Visual Studio 2012 RC since yesterday, We have one WCF solution. Whenever

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.