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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:40:33+00:00 2026-06-09T16:40:33+00:00

I am trying to figure out the cleanest way to implement using a boost::shared_ptr

  • 0

I am trying to figure out the cleanest way to implement using a boost::shared_ptr as a member of a class. Let’s say I have class A that contains a boost::shared_ptr to a class B member. The B class could be either the base type (B) or a derived type (let’s say C or D). This member would be obtained/set continuously throughout the lifetime of class A (using accessors/modifiers). B has to be polymorphic so I can’t just store it as B but rather as a reference/pointer to B. I also wanted to avoid having to manage the memory on my own as well. Let’s say it all looked like:

class A
{
public:
    const B& GetB() const { const B* b = m_pB.get(); return *b; } // Is the correct way to do this?
    void SetB(const B& b) { B* b = m_pB.get(); *b = b; } // Is the correct way to do this?

private:
    boost::shared_ptr<B> m_pB;
};

I know I’m probably doing something wrong but I wanted to provide an example of what I’m trying to accomplish. Thanks!

  • 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-09T16:40:35+00:00Added an answer on June 9, 2026 at 4:40 pm

    I think this program’s design needs rethinking.

    The purpose of a shared pointer is that you leave the dynamically allocated resource within it so that it manages the life-cycle of that resource. If you’re changing what’s in there, then you’re responsible for managing the life of that resource after you replace the pointer in there and that’s going to lead to confusion.

    You should probably just assign over the m_pB variable with a new shared pointer so the original one goes out of scope and frees the resource that was being held in the first place. So, assign over the shared pointer, not over the pointer it holds.

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

Sidebar

Related Questions

I'm trying to figure out the cleanest way to perform some lookups. I have
Trying to figure out the best way to have a templated brochure that creates
I'm trying to figure out the cleanest way to do this. Currently I have
I'm trying to figure out the cleanest way to generate a bit of html
Trying to figure out a way to throw out attributes in this data that
I am trying to figure out what the fastest/cleanest way to sort an array
Trying to figure out the best way to accomplish this. I have inhereted a
Trying to figure out the MVC-concept. I have a NSCustomView that implements drawRect:, which
I'm using Eclipselink and I'm trying figure out the best way to create a
Trying to figure out the best way (either by using what I know in

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.