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);
}
4 个解决方案
#1
28
Pointers to non-static members are a unique beast with unique syntax.
指向非静态成员的指针是具有独特语法的独特野兽。
Those functions need to be supplied a this
pointer, so you must have the Box
pointer handy that will be used as this
.
这些函数需要提供一个this指针,因此你必须使用Box指针,这将被用作这个。
(box->*h)(xPos, yPos, width, height);
#2
8
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的括号,但指向成员语法的指针已经足够特殊了。)
#3
6
From my "award winning" ;-) answer about delegates (available at https://*.com/questions/9568150/what-is-a-c-delegate/9568226#9568226) :
从我的“获奖”;-)关于代表的答案(可在https://*.com/questions/9568150/what-is-a-c-delegate/9568226#9568226获得):
Typedef the pointer to member function like this:
Typedef指向成员函数的指针,如下所示:
typedef void (T::*fn)( int anArg );
Declare one like this:
声明如下:
fn functionPtr = &MyClass::MyFunction
Call it like this:
这样叫:
(MyObject.*functionPtr)( argument );
#4
0
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.
在您给出的示例中,听起来您真正需要的是虚函数。您需要在许多不同类型的对象上调用标准操作(检测是否与对象相交的对象),在编译时无法识别其类型。这是一项针对虚拟功能的工作。
#1
28
Pointers to non-static members are a unique beast with unique syntax.
指向非静态成员的指针是具有独特语法的独特野兽。
Those functions need to be supplied a this
pointer, so you must have the Box
pointer handy that will be used as this
.
这些函数需要提供一个this指针,因此你必须使用Box指针,这将被用作这个。
(box->*h)(xPos, yPos, width, height);
#2
8
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的括号,但指向成员语法的指针已经足够特殊了。)
#3
6
From my "award winning" ;-) answer about delegates (available at https://*.com/questions/9568150/what-is-a-c-delegate/9568226#9568226) :
从我的“获奖”;-)关于代表的答案(可在https://*.com/questions/9568150/what-is-a-c-delegate/9568226#9568226获得):
Typedef the pointer to member function like this:
Typedef指向成员函数的指针,如下所示:
typedef void (T::*fn)( int anArg );
Declare one like this:
声明如下:
fn functionPtr = &MyClass::MyFunction
Call it like this:
这样叫:
(MyObject.*functionPtr)( argument );
#4
0
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.
在您给出的示例中,听起来您真正需要的是虚函数。您需要在许多不同类型的对象上调用标准操作(检测是否与对象相交的对象),在编译时无法识别其类型。这是一项针对虚拟功能的工作。