类方法的指针数组 [英] array of pointers to class methods

查看:63
本文介绍了类方法的指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

希望有人能告诉我怎么做 - 我是一个老''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?

推荐答案

Anonymoose< Ihatespam>写在

新闻:Xn *********************** @ 216.196.97.136:
Anonymoose <Ihatespam> wrote in
news:Xn***********************@216.196.97.136:
你好,
希望有人能告诉我怎么做 - 我是一个老''C''黑客
所以
有时候我会陷入旧思想中...这是其中一次......

我在课堂上有很多方法,我希望能够从一个课程中查找和拨打电话。阵列。在''c''我会做类似的事情:

struct thingie {
char * name;
void(* ptrtofunc)();
} listofthingies [] = {
{" func1",func1},
{" func2",func2}
};

其中func1和func1是返回void的函数,我通常会通过类似的东西调用(例如):

(listofthingies [0] .func1)();
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)();




更正......我的意思是:


(listofthingies [0] .ptrtofunc)();



Correction...I meant:

(listofthingies[0].ptrtofunc)();


" Anonymoose" < Ihatespam>在消息中写道

news:Xn *********************** @ 216.196.97.136 ...
"Anonymoose" <Ihatespam> wrote in message
news:Xn***********************@216.196.97.136...
你好,
希望有人可以告诉我该怎么做 - 我是一个老''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)();


您需要一个对象来调用成员函数。在你的情况下,一个对象是

类型CMyApp。


汤姆。

只是导致术语不评估到一个功能。在这个特定情况下,我的
参考书都没有帮助。

所以,这里有两个问题: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)();
You need an object to call a member function on. In your case, an object of
type CMyApp.

Tom.

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?



" TT \ (Tom Tempelaere \)" < _N ************** @ hotmail.comMAPSO_N_>写在

新闻:TL ****************** @ phobos.telenet-ops.be:
"TT \(Tom Tempelaere\)" <_N**************@hotmail.comMAPSO_N_> wrote in
news:TL******************@phobos.telenet-ops.be:
Anonymoose < Ihatespam>在消息中写道
新闻:Xn *********************** @ 216.196.97.136 ...
"Anonymoose" <Ihatespam> wrote in message
news:Xn***********************@216.196.97.136...
你好,
希望有人可以告诉我怎么做 - 我是一个老''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)();
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)();



你需要一个对象来调用成员函数。在你的情况下,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.


这篇关于类方法的指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆