我尝试在删除器中使用std::shared_pointer。我尝试使用一个成员函数作为删除器。然而,它无法编译。编译器给了我一条错误消息,但我不明白为什么它不能工作。有人知道它为什么不起作用吗?非常感谢。
简化代码如下,
#include <memory>
class MemberFunctionPointerInConstructor {
public:
MemberFunctionPointerInConstructor(void) {
std::shared_ptr<int> a = std::shared_ptr<int>(new int(1), deleter); // this line makes a compiler error message
}
void deleter(int* value) {
delete value;
}
};编译器发出的错误消息如下,
error: invalid use of non-static member function
std::shared_ptr<int> a = std::shared_ptr<int>(new int(1), deleter);
^非常感谢。
发布于 2016-03-04 15:22:33
要使用不绑定到类实例的成员函数,必须声明方法static
static void deleter(int* value) {
delete value;
}https://stackoverflow.com/questions/35799621
复制相似问题