What is the best way to represent a single global class instance?

  architecture, c++, design-patterns

I have a project, which has its own filesystem. The class basically looks like this:

class ResourceManager {
    std::string readFile(std::string const&);
    std::vector<std::string> root;

This class is used everywhere in the project. So it’s global and have a single instance. My current solution is that I create an instance in the main function and then pass it to all of my classes (they store a reference). I’m absolutely fine with this approach, besides that i need to pass the instance to regular functions, but I’m curious if it’s possible to archive a better result.

A simple way of making a filesystem is just to have static functions, because you don’t really need any variables to store. But in my case I have a variable called root, which stores search directories so that I can do something like this:

rm.addResourceRoot(ResourceType::images, "path/to/directory");

I can have a static variable ResourceManager* instance and method ResourceManager& getRm() { return *instance; } inside a class, but that approach feels kind of weird and probably isn’t a "best practice". Probably outside-of-class functions like this will help to make it look better, but I’m still hesitating.

std::string readFile(std::string const& path) {
    return ResourceManager::getRm().readFile(path);

I can use singleton pattern, but everyone convinces me not to use it, because it’s an anti-pattern and not good solution at all. This confuses me in some way so it would be great if anyone could explain the reasons to me.

In my opinion, the ideal access should look like this:


So what is the best way to represent such entities in a project?

Source: Windows Questions C++