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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:00:32+00:00 2026-05-27T00:00:32+00:00

What is a good way to share an instance of an object between several

  • 0

What is a good way to share an instance of an object between several classes in a class hierarchy? I have the following situation:

class texture_manager;

class world {
    ...
    std::vector<object> objects_;
    skybox skybox_;
}

I currently implemented texture_manager as a singleton, and clients call its instancing method from anywhere in the code. texture_manager needs to be used by objects in the objects_ vector, by skybox_, and possibly by other classes as well that may or may not be part of the world class.
As I am trying to limit the use of singletons in my code, do you recommend any alternatives to this approach? One solution that came to mind would be to pass a texture_manager reference as an argument to the constructors of all classes that need access to it. 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-05-27T00:00:32+00:00Added an answer on May 27, 2026 at 12:00 am

    The general answer to that question is to use ::std::shared_ptr. Or if you don’t have that, ::std::tr1::shared_ptr, or if you don’t have that, ::boost::shared_ptr.

    In your particular case, I would recommend one of a few different approaches:

    1. One possibility is, of course, the shared_ptr approach. You basically pass around your pointer to everybody who needs the object, and it’s automatically destroyed when none of them need it anymore. Though if your texture manager is going to end up with pointers to the objects pointing at it, you’re creating a reference cycle, and that will have to be handled very carefully.

    2. Another possibility is just to declare it as a local variable in main and pass it as a pointer or reference to everybody who needs it. It won’t be going away until your program is finished that way, and you shouldn’t have to worry about managing the lifetime. A bare pointer or reference is just fine in this case.

    3. A third possibility is one of the sort of vaguely acceptable uses of something sort of like a singleton. And this deserves a detailed explanation.

    You make a singleton who’s only job is to hand out useful pointers to things. A key feature it has is the ability to tell it what thing to hand out a pointer to. It’s kind of like a global configurable factory.

    This allows you to escape from the huge testing issues you create with a singleton in general. Just tell it to hand out a pointer to a stub object when it comes time to test things.

    It also allows you to escape from the access control/security issue (yes, they create security issues as well) that a singleton represents for the same reason. You can temporarily tell it to pass out a pointer to an object that doesn’t allow access to things that the section of code you’re about to execute doesn’t need access to. This idea is generally referred to as the principle of least authority.

    The main reason to use this is that it saves you the problem of figuring out who needs your pointer and handing it to them. This is also the main reason not to use it, thinking that through is good for you. You also introduce the possibility that two things that expected to get the same pointer to a texture manager actually get pointers to a different texture manager because of a control flow you didn’t anticipate, which is basically the result of the sloppy thinking that caused you to use the Singleton in the first place. Lastly, Singletons are so awful, that even this more benign use of them makes me itchy.


    Personally, in your case, I would recommend approach #2, just creating it on the stack in main and passing in a pointer to wherever it’s needed. It will make you think more carefully about the structure of your program, and this sort of object should probably live for your entire program’s lifetime anyway.

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

Sidebar

Related Questions

In C++, I have an Object class, from which a number of other classes
Can someone share a good way to save the program state (UINavigationController stack, etc)
Does anyone have a good way of deleting duplicate transactions (same date, amount, biller,
Is there a good way in C# to mimic the following python syntax: mydict
Having a object x which is an instance of some class how to create
What is a good way to persist querystring values in asp.net mvc? If I
What would be a good way to determine if a string contains an IPv4
XmlAttributeOverrides is a good way to override some attributes for serialization. MSDN says: You
What's a good way to check if a package is installed while within a
Is there any good way of truncating text with plain HTML and CSS, so

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.