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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:59:11+00:00 2026-06-05T11:59:11+00:00

In an attempt to replace the singleton pattern and a general Resource Manager, I

  • 0

In an attempt to replace the singleton pattern and a general “Resource Manager”, I came with a solution. Making a resource static and protected. That resource is shared between all children from the inherited class. It works, but I wasn’t sure if it’s a good way to proceed.
Here’s a bit of code to express what I’m doing(the resource here is the sf::Texture):

class Foo {
public:
    Foo() {
        if(m_texture == nullptr) {
            //Création et chargement de la texture
            m_texture = std::unique_ptr<sf::Texture>(new sf::Texture());
            m_texture->loadFromFile("...");
        }

    }

    void draw(sf::RenderWindow& window) = 0;

protected:
    static std::unique_ptr<sf::Texture> m_texture = nullptr;

};

class Bar : public Foo {
public:
    Bar()
        : m_sprite(*m_texture) {}

    void draw(sf::RenderWindow& window) {
        window.draw(m_sprite); 
    }

private:
    sf::Sprite m_sprite;
};

That way my resources is shared through all children, and is only initialized once.
Is it a good solution to replace a singleton or a Resource Manager that I would carry everywhere through reference.
Thank you!

  • 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-05T11:59:12+00:00Added an answer on June 5, 2026 at 11:59 am

    Fundamentally what you are tying to do is correct, a static member will be shared (ie be exactly the same) among all the inherited class, this way you only need a single instance which can save you a ton of memory but heres a couple issues … Im assuming you are using g++.

    You can’t initialize non-const member within a class declaration so this.
    static std::unique_ptr<sf::Texture> m_texture = nullptr;
    would produce this:
    error: ISO C++ forbids in-class initialization of non-const static member
    you have to initialize it within the source file of your class, but outside the class.
    std::unique_ptr<sf::Texture> Foo::m_texture = nullptr;

    Second it isn’t save to access member fields directly, always use setters and getters, even within the class functions, this makes the code much more maintainable. As such you can have a static function called getTexture

    static std::unique_ptr<sf::Texture> getTexture() {
        if(m_texture == nullptr) {
            //Création et chargement de la texture
            m_texture = std::unique_ptr<sf::Texture>(new sf::Texture());
            m_texture->loadFromFile("...");
        }
        return m_texture;
    }
    

    while its true that the if statement and function call do add come overhead, this is much more maintainable and safer, and it load the texture at the last minute when its truly needed.

    Back to your question, the Singleton design pattern is quite simple and mostly used to save memory since only a single instance of the object is ever created 🙂 Resource managers are a whole different beast their goal is to centralize all the actions that are required loading and managing resources, combining the two you would initialize a single instance of a resource manager, then access it though a static member field, having all the objects make requests for resources, this could be good or bad, depending on what you are trying to achieve.

    Software design is hard. The best advice I can give is this, when designing a system ask yourself this, “how many lines of code would I have to write to introduce another similar component” your goal should be to minimize this as much as possible, ie reuse as much as possible what you have already created.

    The best programmers are the laziest 🙂 and no I don’t mean copy/paste, that should be banned.

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

Sidebar

Related Questions

Is there any regular expression that will replace everything except alphanumeric? My attempt (not
From the documentation of File.Move : Note that if you attempt to replace a
When I attempt to deserialize: { tags: {} } into: public static class Foo
I know that adb install will not replace an existing package if it's of
The obvious attempt is: Regex.Replace(input, @.$, X, RegexOptions.Singleline); This doesn't always work though. Consider
I'm trying build a regex that will replace any characters not of the format:
How would I replace one pattern with another for every file with extension .cc
I've got a string that has curly quotes in it. I'd like to replace
I would like to replace every line that starts with a certain expression (example:
I have a string that I want to replace the 'xx' with a line

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.