C++ std::bind用于成员函数

使用bind绑定到成员函数时,即使成员函数不需参数,也要将this绑定在第一个参数:

#include <iostream>
#include <functional>
using namespace std;

class test {
public:
	test() : function(std::bind(&test::func, this, 3)) { }    // 需将this绑定在func的第一个参数处
	function<void()> function;
private:
	void func(int i) {
		cout << "in function, the parameter is: " << i << endl;
	}
};

int main() {
	test t;
	t.function();
}

运行它:
在这里插入图片描述

posted @ 2023-04-12 15:17  epiphanyy  阅读(38)  评论(0)    收藏  举报  来源