小编Gog*_*ogo的帖子

Shared_ptr 自定义删除器

我需要为 shared_ptr 做自定义删除器。我知道这可以通过类似的方式完成:

std::shared_ptr<SDL_Surface>(Surf_return_f(), MyDeleter);
Run Code Online (Sandbox Code Playgroud)

但我想按照我的 unique_ptr 自定义删除器的风格制作它们:

struct SDL_Surface_Deleter {
    void operator()(SDL_Surface* surface) {
        SDL_FreeSurface(surface);
    }
};

using SDL_Surface_ptr = std::unique_ptr<SDL_Surface, SDL_Surface_Deleter>;
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

c++ sdl smart-pointers c++11

2
推荐指数
2
解决办法
297
查看次数

从子构造函数/析构函数调用纯虚函数

class A{
virtual void setEnable(bool enable) = 0;
};

class B : A{
    B() {
        setEnable(true);
    }
    ~B() {
        setEnable(false);
    }
    bool enable_ = false;
    
void setEnable(bool enable) override {
    enable_ = enable;
}
};
Run Code Online (Sandbox Code Playgroud)

我是否正确理解 B :: setEnable 函数仅在构造函数退出后才会添加到 vtable 并且这是未定义的行为?

c++

1
推荐指数
1
解决办法
55
查看次数

标签 统计

c++ ×2

c++11 ×1

sdl ×1

smart-pointers ×1