问题描述
你好,
希望有人能告诉我怎么做 - 我是一个老''C''黑客所以
有时候我会陷入困境 - 这是其中一次......
我在课堂上有很多方法,我希望能够获得$>
从数组中查找和调用。在''c''我会做的事情如下:
struct thingie {
char * name;
void(* ptrtofunc)();
} listofthingies [] = {
{" func1",func1},
{" func2",func2}
};
其中func1和func1是返回void的函数,我通常会通过
来调用类似的东西(例如):
(listofthingies [0] .func1)();
这里,我的等价物是(缩写):
struct functable {
CString name;
void(CMyApp :: * wscfunc)();
} functable [] = {
{" send",& CMyApp :: func1},
{" wsasend",& CMyApp :: func2},
};
但是,
(functable [0] .wscfunc)();
只是导致术语不评估函数。在这个特定情况下,我的
参考书都没有帮助。
所以,这里有两个问题:1。解决这个问题的快捷方法是什么(即:可以
我用这种方式引用我的方法吗?),和2.什么是更好的C ++方式来实现同样的事情?
Hello,
Hope someone can tell me how to do this - I''m an old ''C'' hack so
sometimes I get stuck in old-think...this is one of those times...
I''ve got a bunch of methods in a class that I want to be able to
lookup and call from an array. In ''c'' I''d just do something like:
struct thingie {
char *name;
void (*ptrtofunc)();
} listofthingies[] = {
{ "func1", func1 },
{ "func2", func2 }
};
where func1 and func1 are functions that return void that I would usually
call via something like (for example):
(listofthingies[0].func1)();
Here, the equivalent I have is (abbreviated):
struct functable {
CString name;
void (CMyApp::*wscfunc)();
} functable [] = {
{ "send", &CMyApp::func1 },
{ "wsasend", &CMyApp::func2 },
};
But,
(functable[0].wscfunc)();
just results in "term does not evaluate to a function". None of my
reference books help, in this specific case.
So, two questions here: 1. What is the quick way to resolve this (ie: can
I reference my methods this way?), and 2. What is the better C++ way to
accomplish the same thing?
推荐答案
更正......我的意思是:
(listofthingies [0] .ptrtofunc)();
Correction...I meant:
(listofthingies[0].ptrtofunc)();
你需要一个对象来调用成员函数。在你的情况下,CMyApp类型的对象。
Tom。
You need an object to call a member function on. In your case, an
object of type CMyApp.
Tom.
谢谢 - 我找到了其他修复 - 将方法声明为静态。
Thanks - I found the "other" fix too - to declare the methods as static.
这篇关于类方法的指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!