简体   繁体   English

指向函数成员的指针

[英]pointer to function member

I'm trying to do something with pointers to function members that I cant' figure out how this works. 我正在尝试使用指向函数成员的指针,我无法弄清楚它是如何工作的。

I have a class A that looks something like this 我有一个看起来像这样的A级

class A
{
    public:
    float Plus    (float a, float b) { return a+b; }
    float Minus   (float a, float b) { return a-b; }
    float Multiply(float a, float b) { return a*b; }
    float Divide  (float a, float b) { return a/b; }
};

I want to declare a pointer to A and then pass to another function a function pointer to one of the members of A. 我想声明一个指向A的指针然后向另一个函数传递一个函数指针给A的一个成员。

Something likes this: 有点喜欢这个:

void Will_Get_Function_Pointer(float a, float b, float (A::*pt2Func)(float, float))
{
    (...)
}

Where I would call it with something like this 我会用这样的东西来称呼它

A* myA = new A;
Will_Get_Function_Pointer(1.00,2.00, &myA->Minus)

I can't use static/const members because In my final implementation A will point to a specific A in a collection of A objects in something that will look like this 我不能使用static / const成员,因为在我的最终实现中,A将指向A对象集合中的特定A,其中的对象看起来像这样

vector<A> vecA;
myA = &vecA[xxx]

Doing this fails miserably 做到这一点失败了

typedef  float (A::*pA)(float x, float y);
pA p = &myA->Minus;

and the compiler tells me to use &A::Minus but that wont work for me. 编译器告诉我使用&A :: Minus,但这对我不起作用。

Can I even do this? 我甚至可以这样做吗?

Cheers 干杯

A* myA = new A;
Will_Get_Function_Pointer(1.00,2.00, &myA->Minus)

You cannot. 你不能。 You should use something like 你应该使用类似的东西

void Will_Get_Function_Pointer(A* object, float a, float b,
float (A::*pt2Func)(float, float))
{
   (object->*pt2Func)(a, b);
}

And calls it as 并称之为

A* myA = new A;
Will_Get_Function_Pointer(myA, 1.00, 2.00, &A::Minus);

Simple example. 简单的例子。

http://liveworkspace.org/code/79939893695e40f1761d81ba834c5d15 http://liveworkspace.org/code/79939893695e40f1761d81ba834c5d15

Unfortunately, the language doesn't allow you to use that syntax to bind an object to member function to make a callable object. 不幸的是,该语言不允许您使用该语法将对象绑定到成员函数以生成可调用对象。 You can either pass in an object reference (or pointer) and bind it when you call the function: 您可以传入对象引用(或指针)并在调用函数时绑定它:

void Will_Get_Function_Pointer(float a, float b, A & obj, float (A::*pt2Func)(float, float))
{
    (obj.pt2Func)(a,b);
}

Will_Get_Function_Pointer(1.00, 2.00, myA, &A::Minus);

Or you can create a function object that binds the function pointer to the object: 或者您可以创建一个函数对象,将函数指针绑定到对象:

void Will_Get_Function(float a, float b, std::function<float(float,float)> f)
{
    f(a,b);
}

Will_Get_Function(1.00, 2.00, std::bind(&A::Minus, &myA));

Note that function and bind are new in C++11; 请注意, functionbind是C ++ 11中的新增function ; Boost provides equivalents if you're stuck with an old version of the language. 如果您遇到旧版本的语言,Boost会提供等价物。

Use a macro. 使用宏。

#define CALL_MEMBER_FN(object,ptrToMember)  ((object).*(ptrToMember))

Calling your member function is simple because you already have a typedef for the function you want to call. 调用您的成员函数很简单,因为您已经有一个要调用的函数的typedef

float result = CALL_MEMBER_FN(*myA, pA)(a, b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM