C++ 我如何传递一个成员函数,其中需要一个自由函数? 问题如下:考虑这段代码: #include <iostream> class aClass { public: void aTest(int a, int b) { printf("%d + %d = %d", a, b, a + b); } }; void function1(void (*function)(int, int)) { function(1, 1); } void test(int a,int b) { printf("%d - %d = %d", a , b , a - b); } int main() { aClass a; function1(&test); function1(&aClass::aTest); // <-- How should I point to a's aClass::test function? } #包括 类aClass { 公众: 无效aTest(内部a、内部b) { printf(“%d+%d=%d”,a,b,a+b); } }; 无效函数1(无效(*函数)(int,int)) { 功能(1,1); } 无效测试(int a、int b) { printf(“%d-%d=%d”,a,b,a-b); } int main() { a组; 功能1(和测试); function1(&aClass::aTest);//
指向成员函数的指针不同于指向函数的指针。为了通过指针使用成员函数,您需要指向它的指针(显然)和应用它的对象。因此,C++ 我如何传递一个成员函数,其中需要一个自由函数? 问题如下:考虑这段代码: #include <iostream> class aClass { public: void aTest(int a, int b) { printf("%d + %d = %d", a, b, a + b); } }; void function1(void (*function)(int, int)) { function(1, 1); } void test(int a,int b) { printf("%d - %d = %d", a , b , a - b); } int main() { aClass a; function1(&test); function1(&aClass::aTest); // <-- How should I point to a's aClass::test function? } #包括 类aClass { 公众: 无效aTest(内部a、内部b) { printf(“%d+%d=%d”,a,b,a+b); } }; 无效函数1(无效(*函数)(int,int)) { 功能(1,1); } 无效测试(int a、int b) { printf(“%d-%d=%d”,a,b,a-b); } int main() { a组; 功能1(和测试); function1(&aClass::aTest);//,c++,arguments,parameter-passing,function-pointers,pointer-to-member,C++,Arguments,Parameter Passing,Function Pointers,Pointer To Member,指向成员函数的指针不同于指向函数的指针。为了通过指针使用成员函数,您需要指向它的指针(显然)和应用它的对象。因此,function1的适当版本应该是 void function1(void (aClass::*function)(int, int), aClass& a) { (a.*function)(1, 1); } 并称之为: aClass a; // note: no parentheses; with parentheses it's a function decla
function1
的适当版本应该是
void function1(void (aClass::*function)(int, int), aClass& a) {
(a.*function)(1, 1);
}
并称之为:
aClass a; // note: no parentheses; with parentheses it's a function declaration
function1(&aClass::test, a);
使用函数指针没有任何问题。但是,指向非静态成员函数的指针与普通函数指针不同:成员函数需要在作为隐式参数传递给函数的对象上调用。因此,上面的成员函数签名是
void (aClass::*)(int, int)
而不是您尝试使用的类型
void (*)(int, int)
一种方法是将成员函数设置为静态
,在这种情况下,它不需要调用任何对象,您可以将其与类型void(*)(int,int)
一起使用
如果您需要访问类的任何非静态成员,并且需要坚持使用函数指针,例如,因为函数是C接口的一部分,那么最好的选择是始终将void*
传递给函数以获取函数指针,并通过从void*
,然后调用成员函数
在适当的C++接口中,您可能需要查看函数的作用,使用函数对象的模板化参数来使用任意类类型。如果使用模板化的接口是不可取的,则应该使用一些类似于<>代码> STD::函数< /> >:您可以为这些创建一个适当的可调用的函数对象,例如使用<代码> STD::BI。nd()
使用类类型的模板参数或合适的std::function
的类型安全方法比使用void*
接口更可取,因为它们消除了由于转换到错误类型而导致错误的可能性
为了阐明如何使用函数指针调用成员函数,下面是一个示例:
// the function using the function pointers:
void somefunction(void (*fptr)(void*, int, int), void* context) {
fptr(context, 17, 42);
}
void non_member(void*, int i0, int i1) {
std::cout << "I don't need any context! i0=" << i0 << " i1=" << i1 << "\n";
}
struct foo {
void member(int i0, int i1) {
std::cout << "member function: this=" << this << " i0=" << i0 << " i1=" << i1 << "\n";
}
};
void forwarder(void* context, int i0, int i1) {
static_cast<foo*>(context)->member(i0, i1);
}
int main() {
somefunction(&non_member, nullptr);
foo object;
somefunction(&forwarder, &object);
}
//使用函数指针的函数:
void somefunction(void(*fptr)(void*,int,int),void*上下文){
fptr(上下文,17,42);
}
无效非成员(无效*,内部i0,内部i1){
STD::CUT< P> @ Pete Becker的答案是好的,但是你也可以不把<代码>类< /COD>实例作为一个显式参数,而在C++ 11中:<代码>函数1>代码>:< /P>
#include <functional>
using namespace std::placeholders;
void function1(std::function<void(int, int)> fun)
{
fun(1, 1);
}
int main (int argc, const char * argv[])
{
...
aClass a;
auto fp = std::bind(&aClass::test, a, _1, _2);
function1(fp);
return 0;
}
#包括
使用名称空间std::占位符;
void function1(std::function fun)
{
乐趣(1,1);
}
int main(int argc,const char*argv[]
{
...
a组;
auto fp=std::bind(&aClass::test,a,_1,_2);
职能1(fp);
返回0;
}
您现在可以停止胡闹了。这是成员函数的包装器,它支持现有的函数,将普通的C函数作为参数。thread\u local
指令是这里的关键
//示例程序
#包括
#包括
使用名称空间std;
类型def int fooocouse(int);
//现有功能
外部“C”无效厨师(厨师){
cout自2011年起,如果您可以更改功能1
,请这样做:
#include <functional>
#include <cstdio>
using namespace std;
class aClass
{
public:
void aTest(int a, int b)
{
printf("%d + %d = %d", a, b, a + b);
}
};
template <typename Callable>
void function1(Callable f)
{
f(1, 1);
}
void test(int a,int b)
{
printf("%d - %d = %d", a , b , a - b);
}
int main()
{
aClass obj;
// Free function
function1(&test);
// Bound member function
using namespace std::placeholders;
function1(std::bind(&aClass::aTest, obj, _1, _2));
// Lambda
function1([&](int a, int b) {
obj.aTest(a, b);
});
}
#包括
#包括
使用名称空间std;
类aClass
{
公众:
无效aTest(内部a、内部b)
{
printf(“%d+%d=%d”,a,b,a+b);
}
};
模板
void函数1(可调用f)
{
f(1,1);
}
无效测试(int a、int b)
{
printf(“%d-%d=%d”,a,b,a-b);
}
int main()
{
aClass obj;
//自由函数
功能1(和测试);
//有界成员函数
使用名称空间std::占位符;
函数1(std::bind(&aClass::aTest,obj,_1,_2));
//兰姆达
函数1([&](内部a、内部b){
目标测试(a,b);
});
}
()
还请注意,我修复了损坏的对象定义(aClass a();
声明了一个函数)。我问了一个类似的问题(),但我找到的答案更清楚,因此下面是对未来记录的解释:
使用std::函数更容易,如:
void draw(int grid, std::function<void()> element)
甚至更简单:
grid.draw(12, [&]{a.draw()});
创建一个lambda调用通过引用捕获它的对象我将成员函数设置为静态,所有工作:
#include <iostream>
class aClass
{
public:
static void aTest(int a, int b)
{
printf("%d + %d = %d\n", a, b, a + b);
}
};
void function1(int a,int b,void function(int, int))
{
function(a, b);
}
void test(int a,int b)
{
printf("%d - %d = %d\n", a , b , a - b);
}
int main (int argc, const char* argv[])
{
aClass a;
function1(10,12,test);
function1(10,12,a.aTest); // <-- How should I point to a's aClass::test function?
getchar();return 0;
}
#包括
类aClass
{
公众:
静态无效测试(内部a、内部b)
{
printf(“%d+%d=%d\n”,a、b、a+b);
}
};
无效函数1(int a,int b,无效函数(int,int))
{
功能(a,b);
}
无效测试(int a、int b)
{
printf(“%d-%d=%d\n”,a,b,a-b);
}
int main(int argc,const char*argv[]
{
a组;
功能1(10,12,测试);
函数1(10,12,a.aTest);//不确定为什么这个极其简单的解决方案被放弃了:
#包括
类aClass
{
公众:
无效aTest(内部a、内部b)
{
printf(“%d+%d=%d\n”,a、b、a+b);
}
};
模板
void函数1(void(C::*函数)(int,int),C&C)
{
(c.*职能)(1,1);
}
无效函数1(无效(*函数)(int,int)){
功能(1,1);
}
无效测试(int a、int b)
{
printf(“%d-%d=%d\n”,a,b,a-b);
}
int main(int argc,const char*argv[]
{
a组;
功能1(和测试);
函数1(&aClass::aTest,a);
返回0;
}
输出:
1 - 1 = 0
1 + 1 = 2
如果您实际上不需要使用实例a
(即,您可以将其设置为静态,如@mathengineer's)
您可以简单地传入一个非捕获lambda(它会衰减到函数指针)
#包括
类aClass
{
公众:
无效aTest(内部a、内部b)
{
printf(“%d+%d=%d”,a,b,a+b);
}
};
无效函数1(无效(*函数)(int,int))
{
功能(1,1);
}
int main()
{
//注意:你没有
#include <iostream>
class aClass
{
public:
static void aTest(int a, int b)
{
printf("%d + %d = %d\n", a, b, a + b);
}
};
void function1(int a,int b,void function(int, int))
{
function(a, b);
}
void test(int a,int b)
{
printf("%d - %d = %d\n", a , b , a - b);
}
int main (int argc, const char* argv[])
{
aClass a;
function1(10,12,test);
function1(10,12,a.aTest); // <-- How should I point to a's aClass::test function?
getchar();return 0;
}
1 - 1 = 0
1 + 1 = 2
#include <iostream>
class aClass
{
public:
void aTest(int a, int b)
{
printf("%d + %d = %d", a, b, a + b);
}
};
void function1(void (*function)(int, int))
{
function(1, 1);
}
int main()
{
//note: you don't need the `+`
function1(+[](int a,int b){return aClass{}.aTest(a,b);});
}