C++ 调用指向成员函数的指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14814158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:42:46  来源:igfitidea点击:

C++ Call Pointer To Member Function

c++function-pointers

提问by ThingWings

I have a list of pointers to member functions but I am having a difficult time trying to call those functions... whats the proper syntax?

我有一个指向成员函数的指针列表,但是我在尝试调用这些函数时遇到了困难……正确的语法是什么?

typedef void (Box::*HitTest) (int x, int y, int w, int h);

for (std::list<HitTest>::const_iterator i = hitTestList.begin(); i != hitTestList.end(); ++i)
{
    HitTest h = *i;
    (*h)(xPos, yPos, width, height);
}

Also im trying to add member functions to it here

我也试图在这里添加成员函数

std::list<HitTest> list;

for (std::list<Box*>::const_iterator i = boxList.begin(); i != boxList.end(); ++i)
{
    Box * box = *i;
    list.push_back(&box->HitTest);
}

回答by Drew Dormann

Pointers to non-static members are a unique beast with unique syntax.

指向非静态成员的指针是具有独特语法的独特野兽。

Those functions need to be supplied a thispointer, so you must have the Boxpointer handy that will be used as this.

需要为这些函数提供一个this指针,因此您必须准备好Box将用作this.

(box->*h)(xPos, yPos, width, height);

回答by James Kanze

Calling a member function through a pointer to member function has a particular syntax:

通过指向成员函数的指针调用成员函数具有特定的语法:

(obj.*pmf)( params );   //  Through an object or reference.
(ptr->*pmf)( params );  //  Through a pointer.

Although ->*can be overridden, it isn't in the standard library iterators (probably because it would require overrides for every possible function type). So if all you've got is an iterator, you'll have to dereference it and use the first form:

虽然->*可以被覆盖,但它不在标准库迭代器中(可能是因为它需要对每种可能的函数类型进行覆盖)。因此,如果您只有一个迭代器,则必须取消引用它并使用第一种形式:

((*iter).*pmf)( params );

On the other hand, iterating over a the pointer to members themselves doesn't have this problem:

另一方面,迭代指向成员本身的指针没有这个问题:

(objBox.*(*i))( params );   //  If objBox is an object
(ptrBox->*(*i))( params );  //  If ptrBox is a pointer

(I don't think you need the parentheses around the *i, but the pointer to member syntax is already special enough.)

(我认为您不需要在 周围加上括号*i,但是指向成员语法的指针已经足够特殊了。)

回答by Grimm The Opiner

From my "award winning" ;-) answer about delegates (available at https://stackoverflow.com/questions/9568150/what-is-a-c-delegate/9568226#9568226) :

从我的“获奖”;-) 关于代表的回答(可在https://stackoverflow.com/questions/9568150/what-is-ac-delegate/9568226#9568226 获得):

Typedef the pointer to member function like this:

像这样键入指向成员函数的指针:

typedef void (T::*fn)( int anArg );

Declare one like this:

像这样声明一个:

fn functionPtr = &MyClass::MyFunction

Call it like this:

像这样调用它:

(MyObject.*functionPtr)( argument );

回答by Omnifarious

Your attempt to get a member function pointer through an object betrays a misunderstanding. Member function pointers do not include a pointer to the object you call them on. You have to provide such a pointer at the point of the call.

您试图通过对象获取成员函数指针的尝试背叛了误解。成员函数指针不包含指向调用它们的对象的指针。您必须在调用时提供这样的指针。

As many have pointed out, the syntax for a member function call is either:

正如许多人指出的那样,成员函数调用的语法是:

 obj.*funcptr(args);

or

或者

 objptr->*funcptr(args);

In the example you've given, it sounds like what you really need is a virtual function. You have a standard operation (detecting wether or not an object intersects with a box) that needs to be called on many different types of objects, the type of which can't be known at compile time. This is a job that is cut out for virtual functions.

在您给出的示例中,听起来您真正需要的是虚函数。您有一个标准操作(检测对象是否与框相交)需要在许多不同类型的对象上调用,这些对象的类型在编译时是未知的。这是一项为虚拟功能而剪裁的工作。