Using custom deleter with std::shared_ptr

后端 未结 2 1725
盖世英雄少女心
盖世英雄少女心 2020-12-01 03:46

I\'m trying to work out how to use std::shared_ptr with a custom deleter. Specifically, I\'m using it with SDL_Surface as:

std::shared_ptr         


        
2条回答
  •  有刺的猬
    2020-12-01 03:58

    std::shared_ptr(SDL_LoadBMP(....), [=](SDL_Surface* surface)
    {
        std::cout << "Deleting surface\n";
        SDL_FreeSurface(surface);
    });
    

    or

    void DeleteSurface(SDL_Surface* surface)
    {
        std::cout << "Deleting surface\n";
        SDL_FreeSurface(surface);
    }
    
    std::shared_ptr(SDL_LoadBMP(....), DeleteSurface);
    

    EDIT:

    Seeing your updated question, DeleteSurface should be a non-member function, otherwise you need to use std::bind or std::mem_fn or some other member function pointer adapter.

提交回复
热议问题