Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::将参数绑定到没有对象的成员函数_C++_Std Function_Stdbind - Fatal编程技术网

C++ std::将参数绑定到没有对象的成员函数

C++ std::将参数绑定到没有对象的成员函数,c++,std-function,stdbind,C++,Std Function,Stdbind,我需要将一个参数绑定到类成员函数。 大概是这样的: #include <functional> #include <iostream> struct test { void func(int a, int b) { std::cout << a << " " << b << std::endl; } }; int main(int argc, char** argv) {

我需要将一个参数绑定到类成员函数。 大概是这样的:

#include <functional>
#include <iostream>

struct test
{
    void func(int a, int b)
    {
        std::cout << a << " " << b << std::endl;
    }
};

int main(int argc, char** argv)
{
    typedef void (test::*TFunc)(int);
    TFunc func = std::bind(&test::func, 1, std::placeholders::_1);
}

std::bind
不会生成成员函数指针,但它可以生成一个
std::function
对象,供以后使用:

::std::function< void (test *, int)> func = std::bind(&test::func, std::placeholders::_1, 1, std::placeholders::_2);
test t{};
func(&t, 2);
::std::functionfunc=std::bind(&test::func,std::占位符::1,1,std::占位符::2);
测试t{};
func&t,2);

std::bind
不会生成成员函数指针,但它可以生成一个
std::function
对象,供以后使用:

::std::function< void (test *, int)> func = std::bind(&test::func, std::placeholders::_1, 1, std::placeholders::_2);
test t{};
func(&t, 2);
::std::functionfunc=std::bind(&test::func,std::占位符::1,1,std::占位符::2);
测试t{};
func&t,2);

您可能不应该期望由
std::bind
生成的对象可以转换为普通成员函数指针……如果您正在寻找一种在类定义之外基本上定义成员函数的方法,那么这根本无法实现。您只能向类中添加重载或定义自由函数。您可能不应该期望由
std::bind
生成的对象可以转换为普通成员函数指针……如果您正在寻找一种方法来在类定义之外基本上定义成员函数,那么这根本无法实现。您只能向类添加重载或定义自由函数。谢谢您的回答,但您的决定与我的略有不同requirements@tenta4
std::bind
的返回类型始终是(某些)类类型的对象。没有从该指针到成员函数的转换。首先,绑定的
int
无处可去。谢谢您的回答,但您的决定与我的略有不同requirements@tenta4
std::bind
的返回类型始终是(某些)类类型的对象。没有从该指针到成员函数的转换。首先,绑定的
int
无处可去。