在std :: shared_ptr中将成员函数用作自定义删除器时出现问题 [英] Problems using member function as custom deleter with std::shared_ptr

查看:65
本文介绍了在std :: shared_ptr中将成员函数用作自定义删除器时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何将std :: shared_ptr与自定义删除器一起使用.具体来说,我将它与SDL_Surface一起使用:

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<SDL_Surface>(SDL_LoadBMP(....),SDL_FreeSurface);

可以编译并正常运行.但是,我想尝试使用自己的删除器,但无法解决该问题.SDL_FreeSurface的文档位于:

which compiles and runs fine. However, I would like to try out my own deleter and cannot work out how to do so. The documentation for SDL_FreeSurface is found here:

http://sdl.beuc.net/sdl.wiki/SDL_FreeSurface

声明为:

void SDL_FreeSurface(SDL_Surface* surface);

作为测试,并根据该信息,我尝试了以下功能:

As a test, and going by that information, I tried the following function:

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

但是,使用g ++编译会出现以下错误:

However, compiling with g++ gives me the following error:

error: no matching function for call to 'std::shared_ptr<SDL_Surface>::shared_ptr(SDL_Surface*, <unresolved overloaded function type>)'

我看过gcc std :: shared_ptr实现的gnu文档,但是没有太多意义.我在做什么错了?

I have looked at the gnu documentation for the gcc std::shared_ptr implementation but cannot make much sense of it. What am I doing wrong?

此后,我已经缩小了问题的范围,但是将原来的问题保留在上面.我所拥有的是一个Game类,如果我将其简化为一个基本实现,它就像:

I've since narrowed down the problem, but will leave the original question above. What I had was a Game class which, if I strip it down to a basic implementation, was something like:

class Game {
    public:
        /* various functions */
    private:
        void DeleteSurface(SDL_Surface* surface);
        bool CacheImages();
        std::vector<std::shared_ptr<SDL_Surface> > mCachedImages;

        /* various member variables and other functions */
}

使用上述 DeleteSurface 的实现,以及 CacheImages()的实现如下:

with the implementation of DeleteSurface as above, and the implementation of CacheImages() as:

bool CacheImages()
{
    mCachedImages.push_back(std::shared_ptr<SDL_Surface>(SDL_LoadBMP(...),DeleteSurface);
    return true;
}

哪个游戏对我来说我上面列出的错误.但是,如果我将 DeleteSurface()函数移到 Game 类之外而不进行其他更改,则代码会编译.在会导致问题的 Game 类中包含 DeleteSurface 函数是什么意思?

which game me the error I listed above. However, if I move the DeleteSurface() function outside the Game class without otherwise altering it, the code compiles. What is it about including the DeleteSurface function in the Game class that is causing problems?

推荐答案

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

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

std::shared_ptr<SDL_Surface>(SDL_LoadBMP(....), DeleteSurface);

看到更新后的问题, DeleteSurface 应该是非成员函数,否则您需要使用 std :: bind std :: mem_fn 或其他一些成员函数指针适配器.

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.

这篇关于在std :: shared_ptr中将成员函数用作自定义删除器时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆