Search code examples
c++c++11sdlsmart-pointersraii

Initialization list in singleton class


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.


Solution

  • You can still use the member initializer list:

    Game::Game()
        : window_(SDL_CreateWindow(...), SDL_DestroyWindow)
    {
    }