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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:12:08+00:00 2026-05-14T01:12:08+00:00

I was browsing for an alternative to using so many shared_ptrs, and found an

  • 0

I was browsing for an alternative to using so many shared_ptrs, and found an excellent reply in a comment section:

Do you really need shared ownership?
If you stop and think for a few
minutes, I’m sure you can pinpoint one
owner of the object, and a number of
users of it, that will only ever use
it during the owner’s lifetime. So
simply make it a local/member object
of the owners, and pass references to
those who need to use it.

I would love to do this, but the problem becomes that the definition of the owning object now needs the owned object to be fully defined first. For example, say I have the following in FooManager.h:

class Foo; 
class FooManager
{
    shared_ptr<Foo> foo;
    shared_ptr<Foo> getFoo() { return foo; }
};

Now, taking the advice above, FooManager.h becomes:

#include "Foo.h"
class FooManager
{
    Foo foo;
    Foo& getFoo() { return foo; }
};

I have two issues with this. First, FooManager.h is no longer lightweight. Every cpp file that includes it now needs to compile Foo.h as well. Second, I no longer get to choose when foo is initialized. It must be initialized simultaneously with FooManager. How do I get around these issues?

  • 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-14T01:12:08+00:00Added an answer on May 14, 2026 at 1:12 am

    You can use a shared_ptr ( or any smart pointer, or even a dumb pointer) but not have shared ownership.

    E.g.

    class Foo; 
    class FooManager
    {
      private:
        shared_ptr<Foo> foo;
      public:
        Foo& getFoo() { return *foo; }
    };
    

    (This is just a sketch – you still need a setFoo(), and perhaps getFoo() should return a Foo *. But the point is that you’re back to being lightweight, and you can control when foo is created.)

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

Sidebar

Related Questions

Browsing through the channel 9 msdn videos I found the following unanswered comment and
Browsing the Internet, I found the new Ample SDK JavaScript framework. From their about
When browsing ASP.NET MVC source code in codeplex , I found it is common
While browsing some code I found a call to OpenPrinter() . The code compiles
While browsing System.Zip (Delphi XE2) to see how it works, I found this function:
Browsing through the source code of Microsoft's sample StockTrader application, I found this snippet
Browsing through the 2.0-wip branch of Twitter Bootstrap, I found LESS have a nice
While browsing questions and answers in this forum i found a piece of code
I was browsing through a webpage which had some c FAQ's, I found this
Browsing the Linux kernel sources I found some piece of code where a block

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.