类 C++ 中的函数指针表
我正在尝试在类中创建一个函数指针表。我在网上找不到任何这样的例子,大多数都涉及在类之外使用成员函数指针。
例如:
class Test
{
typedef void (Test::*FunctionType)();
FunctionType table[0x100];
void TestFunc()
{
}
void FillTable()
{
for(int i = 0; i < 0x100; i++)
table[i] = &Test::TestFunc;
}
void Execute(int which)
{
table[which]();
}
}test;
给我错误“术语不计算为采用 0 个参数的函数”。
I'm trying to make a table of function pointers within a class. I haven't been able to find any examples of this online, most involve using member function pointers outside of their class.
for example:
class Test
{
typedef void (Test::*FunctionType)();
FunctionType table[0x100];
void TestFunc()
{
}
void FillTable()
{
for(int i = 0; i < 0x100; i++)
table[i] = &Test::TestFunc;
}
void Execute(int which)
{
table[which]();
}
}test;
Gives me the error "term does not evaluate to a function taking 0 arguments".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
Execute
函数中的这一行中:您不能这样调用它,因为它不是一个普通函数。您必须为它提供一个要操作的对象,因为它是一个指向成员函数的指针,而不是指向函数的指针(有区别):
这将使调用对象成为任何一个对象由
this
指针(正在执行Execute
的指针)指向。另外,在发布错误时,请确保包含发生错误的行。
In this line in the
Execute
function:You can't call it like that because it's not a normal function. You have to provide it with an object on which to operate, because it's a pointer to a member function, not a pointer to a function (there's a difference):
That will make the invoking object whichever object is pointed to by the
this
pointer (the one that's executingExecute
).Also, when posting errors, make sure to include the line on which the error occurs.
赛斯有正确的答案。下次,在 MSDN 上查找编译器错误号,您将看到相同的内容: 编译器错误 C2064。
Seth has the right answer. Next time, look up the compiler error number on MSDN and you'll see the same: Compiler Error C2064.
您需要一个调用函数的上下文。在您的情况下,上下文是
this
:You need a context in which to call your function. In your case, the context is
this
: