我正在尝试使用带有自定义删除器的唯一指针创建一个包装类。这是类(class):
class WindowObj
{
public:
WindowObj();
WindowObj(const WindowObj&) = delete;
WindowObj& operator=(const WindowObj&) = delete;
WindowObj(WindowObj&&);
WindowObj& operator=(WindowObj&&);
WindowObj(const char* title,int x,int y,const int& w,const int& h,SDL_WindowFlags flags);
SDL_Window* get();
protected:
private:
std::unique_ptr<SDL_Window,decltype(&SDL_DestroyWindow)> window;
};
WindowObj::WindowObj(const char* title,int x,int y,const int& w,const int& h,SDL_WindowFlags flags)
{
window = make_resource(SDL_CreateWindow,SDL_DestroyWindow,title,x,y,w,h,flags);
}
SDL_Window* WindowObj::get(){
return window.get();
}
WindowObj::WindowObj(){
window = NULL;
}
WindowObj::WindowObj(WindowObj&& other){
std::swap(window,other.window);
}
WindowObj& WindowObj::operator=(WindowObj&& other){
std::swap(window,other.window);
}
问题是,当我尝试编译它时,我收到错误错误:静态断言失败:使用空函数指针删除器构建
。使用shared_ptr 可以解决问题,但我可能不需要共享指针。有什么帮助吗?
最佳答案
您没有初始化窗口
,而是在第一次默认构造它之后对其进行分配(使用空删除器,因此会出现错误)。
您需要使用成员初始值设定项列表来实际初始化它:
WindowObj::WindowObj(const char* title,int x,int y,const int& w,const int& h,SDL_WindowFlags flags)
: window(make_resource(SDL_CreateWindow,SDL_DestroyWindow,title,x,y,w,h,flags))
{
}
(假设 make_resource
返回一个 unique_ptr
并将第二个参数传递给其构造函数。)
关于C++ 带有自定义删除器错误的唯一指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36523967/