0

I decided to rewrite my code and replace all raw pointers with thinks like smart pointers or references. However, I am using singleton pattern for some of my classes (Game, EntityManager, Input...) and dont know ho wo initialize smart pointers. The problem is I use SDL and I need to set the deletor of the smart pointer

std::unique_ptr<SDL_Window> window_(SDL_CreateWindow(...), SDL_DestroyWindow);

This is how I would normaly do it, but I do not know how to do it when the pointers is private member of singleton class and I can not pass any arguments to the constructor of the class (like window name, width, height...).

class Game
{
private:
    std::unique_ptr<SDL_Window> window_;

    Game();
    ~Game();

public:
    static Game& getInstance();
};

Thanks for answers.

davidv
  • 373
  • 5
  • 14

1 Answers1

0

You can still use the member initializer list:

Game::Game()
    : window_(SDL_CreateWindow(...), SDL_DestroyWindow)
{
}
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • Oh I maybe did not write the question right, I know I can, but this way the problem is I can not pass any arguments to the game from int main() at least I do not know how. – davidv Sep 01 '13 at 14:44
  • 1
    Well, you can leave the instance uninitialized and initialize it in a separate method, e.g., `setupSingleton()` and throw an exception if the singleton is accessed while it isn't set up. BTW, Singleton is an anti-pattern and has very few legitimate uses. Your use seems to assume that calling global variables a singleton makes them blessed somehow! It doesn't. – Dietmar Kühl Sep 01 '13 at 14:48