std::具有非静态成员函数的函数
std::function with non-static member functions
我试图理解一个概念和一个错误。这个怎么了?
class A
{
public:
A()
{
std::function<void(int)> testFunc(&A::func);
}
private:
void func(int) {}
}
我的问题是,是否可以创建任何类型的对象来调用特定实例的成员,其中std::function的作用就像一个成员函数指针,除非没有古怪的类型定义,否则不能在继承类中用作函数参数。例如:
class A
{
public:
A()
{
index[WM_CREATE] = &A::close;
index[WM_DESTROY] = &A::destroy;
}
protected:
map<UINT msg, void (A::*)(HWND, UINT , WPARAM, LPARAM)> index;
void close(HWND,UINT, WPARAM, LPARAM);
void destroy(HWND, UINT, WPARAM, LPARAM);
};
class B : public A
{
public:
B()
{
index[WM_CREATE] = &B::create; // error because it's not a pointer of type A::*
}
private:
void create(HWND, UINT, WPARAM, LPARAM);
};
我认为我正在正确地使用std::函数,比如so:
class A
{
public: // Gigantic stl error with these two
A() // |
{ // V
index[WM_CREATE] = std::function<void(HWND, UINT, WPARAM, LPARAM>(&A::close);
index[WM_DESTROY] = std::function<void(HWND, UINT, WPARAM, LPARAM>(&A::destroy);
}
protected:
map<UINT msg, std::function<void(HWND, UINT, WPARAM, LPARAM)> > index;
void close(HWND,UINT, WPARAM, LPARAM);
void destroy(HWND, UINT, WPARAM, LPARAM);
};
class B : public A
{
public: // and this one
B() // |
{ // V
index[WM_CREATE] = std::function<void(HWND, UINT, WPARAM, LPARAM)>(&B::create);
}
private:
void create(HWND, UINT, WPARAM, LPARAM);
};
如果有人能解释这些巨大的神秘错误意味着什么以及如何修复它们,我将不胜感激
我认为您遇到的问题是,成员函数不仅需要一个函数指针,还需要一个指向调用对象的指针。换句话说,成员函数有一个附加的隐式参数,它是指向调用对象的指针。
要将成员函数设置为std::函数,需要使用std::bind,如下所示:
std::function<void(int)> testFunc(std::bind(&A::func, this, _1));
这将当前A实例的This指针绑定到函数,因此它具有函数指针和对象实例,这些信息足以正确调用函数。_1参数表示在调用函数时将提供第一个显式参数。
使用c++11,您还可以使用比std::bind
:更容易读取的lambda
index[WM_CREATE] = [this](HWND h, UINT u, WPARAM w, LPARAM l)
{
create(h, u, w, l);
}
我的问题是,是否可以创建任何类型的对象来调用特定实例的成员
在这种情况下,唯一缺少的信息实际上是std::function
对象应该使用哪个特定实例:&A::func
不能单独使用(例如(this->*&A::func)(0)
将&A::func
与实例*this
一起使用)。尝试:
std::function<void(int)> testFunc = std::bind(&A::func, this);
(注意std::bind(&A::func, *this)
和std::bind(&A::func, this)
的语义略有不同。)
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 如何在C++中使用非静态成员函数作为回调函数
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- 静态成员函数使用相同的名称时出现模板类型名称错误
- 在 nullptr 上调用无状态类的非静态成员函数是否合法?
- 如何在友元函数中使用静态成员而不添加前缀 [类名]::
- C++构造函数和静态成员
- 为什么传递非静态成员函数会导致编译错误?
- 非静态成员失败的线程调用函数
- 处理类内的回调时,必须调用对非静态成员函数的引用
- 非静态成员函数的 decltype 格式不正确吗?
- 为什么 std::sort 找不到合适的(静态成员)函数重载?
- std::异步与非静态成员函数
- C++无效使用非静态成员函数?
- 指向重载静态成员的函数指针 - 在unique_ptr中用作自定义删除器
- 未使用的C++未优化的静态成员函数/变量
- C++:如何返回指向非静态成员函数的指针?
- 有没有一种方法可以使全局函数/静态成员函数一次可呼出
- 函数静态成员变量
- 隐藏模板化的辅助函数——静态成员或未命名的命名空间