Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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++ 指向类成员函数的函数指针_C++ - Fatal编程技术网

C++ 指向类成员函数的函数指针

C++ 指向类成员函数的函数指针,c++,C++,我想做一个函数,它有函数指针作为参数 #include <iostream> using namespace std; class test{ public: test(){}; double tt(double input){ return input; }; }; double fptr_test(double (*fptr)(double), double input){ return fptr(input); }

我想做一个函数,它有函数指针作为参数

#include <iostream>
using namespace std;

class test{

public:
    test(){};

    double tt(double input){
        return input;
    };

};

double fptr_test(double (*fptr)(double), double input){
    return fptr(input);
}


int main(){

    test t;
    cout << t.tt(3) << endl;
    cout << fptr_test(t.tt, 3) << endl;  // This line doesn't work
    cout << fptr_test(&test::tt, 3) << endl;  // This line can't compile

    return 1;
}
#包括
使用名称空间std;
课堂测试{
公众:
test(){};
双tt(双输入){
返回输入;
};
};
双fptr_测试(双(*fptr)(双),双输入){
返回fptr(输入);
}
int main(){
试验t;

cout如果要将指针传递给成员函数,则需要使用成员函数指针,而不是泛型自由函数的指针和调用它的对象

两者都不是可选的

double fptr_test(test& t, double (test::*fptr)(double), double input){
    return t.*fptr(input);
}

// call like this:
fptr_test(&test::tt, 3); // Your second try

如果要向成员函数传递指针,则需要使用成员函数指针,而不是泛型自由函数的指针和调用它的对象

两者都不是可选的

double fptr_test(test& t, double (test::*fptr)(double), double input){
    return t.*fptr(input);
}

// call like this:
fptr_test(&test::tt, 3); // Your second try

这是修改后的代码

#include <iostream>
using namespace std;

class test{

public:
    test(){};

    double tt(double input){
        return input;
    };

};

double fptr_test(test* t, double (test::*fptr)(double), double input){
    return (t->*fptr)(input);
}

int main(){

    test t;
    cout << t.tt(3) << endl;
    cout << fptr_test(&t, &test::tt, 3) << endl;

    return 1;
}
#包括
使用名称空间std;
课堂测试{
公众:
test(){};
双tt(双输入){
返回输入;
};
};
双fptr_测试(测试*t,双(测试:*fptr)(双),双输入){
返回(t->*fptr)(输入);
}
int main(){
试验t;

cout这是修改后的代码

#include <iostream>
using namespace std;

class test{

public:
    test(){};

    double tt(double input){
        return input;
    };

};

double fptr_test(test* t, double (test::*fptr)(double), double input){
    return (t->*fptr)(input);
}

int main(){

    test t;
    cout << t.tt(3) << endl;
    cout << fptr_test(&t, &test::tt, 3) << endl;

    return 1;
}
#包括
使用名称空间std;
课堂测试{
公众:
test(){};
双tt(双输入){
返回输入;
};
};
双fptr_测试(测试*t,双(测试:*fptr)(双),双输入){
返回(t->*fptr)(输入);
}
int main(){
试验t;

cout函数指针和成员函数指针的类型不兼容。例如,
&test::tt
的类型为

double (test::*)(double)
而不是

double (*)(double)
造成这种差异的原因是[非
静态
]成员函数有一个隐藏参数:指向成员函数所应用的对象的指针,即
this
。从成员函数中导出正常函数指针的方法是通过提供
this
指针的函数进行委托,从而获得一个额外的参数

<>在C++中,函数指针不可作为函数的参数,而函数可以自定义,而是采用函数对象。
  • 快速的方法是将函数对象类型设为模板参数,并直接传递得到的函数对象。例如,
    fptr\u test()
    如下所示:

    template <typename Fun>
    double fptr_test(Fun fun, double input) {
        return fun(input);
    }
    
  • 在这两种情况下,函数对象只接受一个参数,而您的成员函数接受两个参数:调用函数的对象和
    double
    参数。您需要
    std::bind(…)
    将第一个参数绑定到对象,并将结果对象传递到
    fptr_test()

    测试对象;
    
    std::cout函数指针和成员函数指针的类型不兼容。例如,
    &test::tt
    的类型为

    double (test::*)(double)
    
    而不是

    double (*)(double)
    
    造成这种差异的原因是[非
    静态
    ]成员函数有一个隐藏参数:指向成员函数所应用的对象的指针,即
    this
    。从成员函数中导出正常函数指针的方法是通过提供
    this
    指针的函数进行委托,从而获得一个额外的参数

    <>在C++中,函数指针不可作为函数的参数,而函数可以自定义,而是采用函数对象。
  • 快速的方法是将函数对象类型设为模板参数,并直接传递得到的函数对象。例如,
    fptr\u test()
    如下所示:

    template <typename Fun>
    double fptr_test(Fun fun, double input) {
        return fun(input);
    }
    
  • 在这两种情况下,函数对象只接受一个参数,而您的成员函数接受两个参数:调用函数的对象和
    double
    参数。您需要
    std::bind(…)
    将第一个参数绑定到对象,并将结果对象传递到
    fptr_test()

    测试对象;
    
    你可能想要的是:

    #include <iostream>
    #include <functional>
    using namespace std;
    
    class test{
    
    public:
        test(){};
    
        double tt(double input){
            return input;
        };
    
    };
    
    double fptr_test( std::function<double(double)> func, double input){
        return func(input);
    }
    
    
    int main(){
        using namespace std::placeholders;
    
        test t;
        cout << t.tt(3) << endl;
        cout << fptr_test( std::bind( &test::tt, t, _1 ), 3) << endl;
    
        return 0;
    }
    
    #包括
    #包括
    使用名称空间std;
    课堂测试{
    公众:
    test(){};
    双tt(双输入){
    返回输入;
    };
    };
    双fptr_测试(标准::函数func,双输入){
    返回函数(输入);
    }
    int main(){
    使用名称空间std::占位符;
    试验t;
    
    cout您可能想要的是:

    #include <iostream>
    #include <functional>
    using namespace std;
    
    class test{
    
    public:
        test(){};
    
        double tt(double input){
            return input;
        };
    
    };
    
    double fptr_test( std::function<double(double)> func, double input){
        return func(input);
    }
    
    
    int main(){
        using namespace std::placeholders;
    
        test t;
        cout << t.tt(3) << endl;
        cout << fptr_test( std::bind( &test::tt, t, _1 ), 3) << endl;
    
        return 0;
    }
    
    #包括
    #包括
    使用名称空间std;
    课堂测试{
    公众:
    test(){};
    双tt(双输入){
    返回输入;
    };
    };
    双fptr_测试(标准::函数func,双输入){
    返回函数(输入);
    }
    int main(){
    使用名称空间std::占位符;
    试验t;
    简短回答:“指向函数的指针”和“指向成员函数的指针”都存在,但它们并不相同,也不能用一个代替另一个。简短回答:“指向函数的指针”和“指向成员函数的指针”都存在,但它们不相同,也不能用一个代替另一个。