【发布时间】:2012-11-14 00:19:19
【问题描述】:
我已将几个成员函数分组到一个数组中。如何从数组中访问函数?我收到“错误 C2064:术语不评估为采用 0 个参数的函数。”见下文。
class A
{
public:
//Constructor
A()
{
//Fill function array
ClipFunction[0] = &A::ClipTop;
ClipFunction[1] = &A::ClipBottom;
ClipFunction[2] = &A::ClipLeft;
ClipFunction[3] = &A::ClipRight;
}
//Declare array
typedef void (A::*ClipFunction_ptr) ();
ClipFunction_ptr ClipFunction[4];
//Clipping functions
void ClipTop();
void ClipBottom();
void ClipLeft();
void ClipRight();
//Start clipping process
void StartClip();
};
//Define clipping functions
void A::ClipTop() {}
void A::ClipBottom() {}
void A::ClipLeft() {}
void A::ClipRight() {}
//Define A::StartClip()
void A::StartClip()
{
//Run through all functions in the array
for (unsigned int i = 0; i < 4; i++)
{
ClipFunction[i](); //ERROR. How do I access ClipFunction[i] ???
}
}
【问题讨论】:
-
一开始你为什么要做这么丑陋的事情? :( 我认为你可以在这样的任务中使用en.cppreference.com/w/cpp/utility/functional/function,如果真的需要的话......另外,我觉得你当前的实现永远不会释放
ClipFunction数组...... -
@MihaiTodor 你说得对!我没有释放数组。
-
这是正确的,因为数组不是动态分配的,所以它不必被释放。 stackoverflow.com/questions/1879431/how-to-destruct-an-array
-
@IvanVergiliev 是的,你是对的。对此感到抱歉。
标签: c++ arrays function pointers member