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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:53:45+00:00 2026-06-14T02:53:45+00:00

I love Boost’s smart_ptr features and the ability to convert to and from a

  • 0

I love Boost’s smart_ptr features and the ability to convert to and from a shared_ptr and weak_ptr, but since the reference count is not contained in the pointed class itself, the following code does not work (and it shouldn’t).

A *a = new A;
shared_ptr<A> aPtr1(a);

{
    shared_ptr<A> aPtr2(a);

    // The reference counts of aPtr1 and aPtr2 are both 1.
}   // At this point, `a` is destructed by aPtr2.

aPtr1->foo();   // And... SIGTERM

I believe the JUCE framework has this functionality. [ReferenceCountedObject and ReferenceCountedObjectPtr]
However, I’d rather use Boost for my application. Is it possible to allow Boost smart_ptrs to look for the reference count in the pointed class rather than the private boost::detail::shared_count instance?

  • 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-14T02:53:46+00:00Added an answer on June 14, 2026 at 2:53 am

    Simple solution:

    A *a = new A;
    shared_ptr<A> aPtr1(a);
    {
        // construct new shared pointer from old one.
        shared_ptr<A> aPtr2(aPtr1);
    }
    aPtr1->foo();
    

    If you want something more complicated, see http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html :

    The header <boost/enable_shared_from_this.hpp> defines the class template enable_shared_from_this. It is used as a base class that allows a shared_ptr to the current object to be obtained from within a member function.


    Edit: I should have mentioned that enable_shared_from_this has some unfortunate issues with derivation. However, the following works with c++11; I didn’t try it with boost but I suppose it should work there, too. I think it’s a bit of a hack; using raw pointers when you use shared_ptr’s is bound to end in tears:

    struct X : std::enable_shared_from_this {
      /* stuff */
    };
    
    struct Y : X {
      /* stuff */
      void yonly() {};
    };
    
    int main() {
      Y* y = new Y;
      shared_ptr<Y> sy(y);
      sy->yonly();
      {
        auto sy2 = std::shared_ptr<Y>(y->shared_from_this(), y);
        sy2->yonly();
      } 
      std::cout << "Block ended" << std::endl;
      return 0;
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a long time user of the boost::smart_ptr library and love it. Like all
I love the ability to define a parseAction with pyarsing , but I've run
I love paredit. But there are a couple of things I hate, and have
I love LINQ to SQL but it has been bugging me that in using
I love NHibernate's ability to have one table to store multiple types based on
I love Ruby and its framework, but I don't think that Ruby On Rails
I love the idea of Grizzly, but I can't find any good examples to
Since I love progamming in both C# and C++, I'm about to implementing a
I love Eclipse's quickfixes. I use the assign parameter to new field often, but
I love the new fisheye plug in (http://bost.ocks.org/mike/fisheye/) but want to get it working

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.