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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:12:48+00:00 2026-05-17T15:12:48+00:00

There are several interesting questions on pitfalls with boost::shared_ptr s. In one of them,

  • 0

There are several interesting questions on pitfalls with boost::shared_ptrs. In one of them, there is the useful tip to avoid pointing boost::shared_ptr<Base> and boost::shared_ptr<Derived> to the same object of type Derived since they use different reference counts and might destroy the object prematurely.

My question: Is it safe to have both boost::shared_ptr<T> and boost::shared_ptr<const T> point to the same object of type T, or will this cause the same problem?

  • 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-17T15:12:48+00:00Added an answer on May 17, 2026 at 3:12 pm

    It is perfectly safe.

    The following code sample:

    #include <iostream>
    #include <boost/shared_ptr.hpp>
    
    int main(int, char**)
    {
      boost::shared_ptr<int> a(new int(5));
      boost::shared_ptr<const int> b = a;
    
      std::cout << "a: " << a.use_count() << std::endl;
      std::cout << "b: " << b.use_count() << std::endl;
    
      return EXIT_SUCCESS;
    }
    

    Compiles and run fine, and is perfectly correct. It outputs:

    a: 2
    b: 2
    

    The two shared_ptr share the same reference counter.


    Also:

    #include <iostream>
    #include <boost/shared_ptr.hpp>
    
    class A {};
    class B : public A {};
    
    int main(int, char**)
    {
        boost::shared_ptr<A> a(new B());
        boost::shared_ptr<B> b = boost::static_pointer_cast<B>(a);
    
        std::cout << "a: " << a.use_count() << std::endl;
        std::cout << "b: " << b.use_count() << std::endl;
    
        return EXIT_SUCCESS;
    }
    

    Behave the same way. You must, however never build your shared_ptr using a construct like this:

    boost::shared_ptr<A> a(new B());
    boost::shared_ptr<B> b(static_cast<B*>(a.get()));
    

    a.get() gives the raw pointer and loses all information about reference counting. Doing this, you’ll end up with two distinct (not linked) shared_ptr that use the same pointer but different reference counters.

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

Sidebar

Related Questions

There are several threads on this here at SO but I didn't find one
There are several other questions about this topic that I have gone through, but
There are several variant questions on this on SO, but I did not find
There are several useful answers on SO regarding prevention of brute forcing a password
I've recently discovered that there are several useful templates (in Eclipse) that can be
I have one image of house and in house there are several parts available
There are several places on the internet that talk about having multy key dictionary
There are several ways google throws at me for checking if a file is
There are several good Javascript editors for Markdown / Textile (e.g.: http://attacklab.net/showdown/ , the
There are several resources out there that explain how the sandbox in Chrome works

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.