I have a class where I try to create static unique_ptr to the SDL_Renderer:
.h
class Game
{
public:
static std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer*)>> Renderer;
// ...
};
.cpp
std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer*)>> Game::Renderer;
void Game::Initialize(int width, int height)
{
// Some code here //
// Initialization OPTION 1
Renderer = std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer*)>>
(SDL_CreateRenderer(Window.get(), -1, 0), SDL_DestroyRenderer);
return;
}
Questions are:
- Is it the right way to initialize the unique_ptr here? (at a glance code works as expected)
- Is it possible to use somehow make_unique instead?
- Will the smart pointer work correctly and call SDL_DestroyRenderer after Game deletion?
- Is it safe and corresponds to the best practices to use smart-pointers with C-libs classes/structs?