473,499 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array of pointers to class methods

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?
Nov 17 '05 #1
10 1599
Anonymoose <Ihatespam> wrote in
news:Xn***********************@216.196.97.136:
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)();


Correction...I meant:

(listofthingies[0].ptrtofunc)();
Nov 17 '05 #2
"Anonymoose" <Ihatespam> wrote in message
news:Xn***********************@216.196.97.136...
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?

Nov 17 '05 #3
"TT \(Tom Tempelaere\)" <_N**************@hotmail.comMAPSO_N_> wrote in
news:TL******************@phobos.telenet-ops.be:
"Anonymoose" <Ihatespam> wrote in message
news:Xn***********************@216.196.97.136...
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.


Thanks - I found the "other" fix too - to declare the methods as static.
Nov 17 '05 #4
"Anonymoose" <Ihatespam> wrote in message
news:Xn***********************@216.196.97.136...
"TT \(Tom Tempelaere\)" <_N**************@hotmail.comMAPSO_N_> wrote in
news:TL******************@phobos.telenet-ops.be:
"Anonymoose" <Ihatespam> wrote in message
news:Xn***********************@216.196.97.136...
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.


Thanks - I found the "other" fix too - to declare the methods as static.


That is indeed the 'other' fix, because you didn't really need an object to
operate on. This is how you could do it for member methods (just an
example).

struct my_class {
int i;
my_class(int i_) : i(i_) {}
int increment() { return ++i; }
int decrement() { return --i; }
};

struct callable {
typedef int (my_class::* callable_fn)();

my_class mc;
callable_fn the_func;

callable(const my_class& mc_, callable_fn the_func_)
: mc(mc_), the_func(the_func_)
{}
int operator()() {
return (mc.*the_func)();
}
};

int main() {
callable cl[] = {
callable( my_class(5), &my_class::increment ),
callable( my_class(7), &my_class::decrement )
};
for(int i=0; i != (sizeof(cl)/sizeof(*cl)); ++i)
std::cout << cl[i]() << std::endl;

std::swap( cl[0].the_func, cl[1].the_func );

while( cl[0].mc.i != 0 )
for(int i=0; i != (sizeof(cl)/sizeof(*cl)); ++i)
cl[i]();
}

Tom.
Nov 17 '05 #5
Anonymoose wrote:
"TT \(Tom Tempelaere\)" <_N**************@hotmail.comMAPSO_N_> wrote in
news:TL******************@phobos.telenet-ops.be:
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.


Thanks - I found the "other" fix too - to declare the methods as static.


Static methods are less useful than non-static (there's no inheritance
and you can't have more than one behaviorally different set of
methods). I believe there's a more elegant solution for your design,
but more background data is needed. What should this class do?

In the worst case I would make a class with only one virtual public
function, with only one parameter LPCTSTR. This function would do its
thing based on this parameter (whether it's "send", "wsasend", or
something else). Anyway, how is it possible you don't need any
parameters for your functions?

Nov 17 '05 #6
Mihajlo Cvetanovic <ma*@RnEeMtOsVeEt.co.yu> wrote:
[...] I believe there's a more elegant solution for your design,
but more background data is needed. What should this class do?
It looks like Anonymoose is trying
to re-invent virtual functions. When
I saw the posting I wondered why one
wouldn't use them for this task.
[...]


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #7
"Hendrik Schober" <Sp******@gmx.de> wrote in news:epnSyDx6DHA.2560
@TK2MSFTNGP09.phx.gbl:
Mihajlo Cvetanovic <ma*@RnEeMtOsVeEt.co.yu> wrote:
[...] I believe there's a more elegant solution for your design,
but more background data is needed. What should this class do?


It looks like Anonymoose is trying
to re-invent virtual functions. When
I saw the posting I wondered why one
wouldn't use them for this task.


Cause he doesn't understand them that well (yet). The task, is reading
lines of input, parsing them, and calling one of a set of functions with
the parsed arguments, based on what the arguments are.
Nov 17 '05 #8
Anonymoose <Ihatespam> wrote:
"Hendrik Schober" <Sp******@gmx.de> wrote in news:epnSyDx6DHA.2560
@TK2MSFTNGP09.phx.gbl:
Mihajlo Cvetanovic <ma*@RnEeMtOsVeEt.co.yu> wrote:
[...] I believe there's a more elegant solution for your design,
but more background data is needed. What should this class do?
It looks like Anonymoose is trying
to re-invent virtual functions. When
I saw the posting I wondered why one
wouldn't use them for this task.


Cause he doesn't understand them that well (yet).


They do pretty much exactly what you are
trying to do. Only that compilers rarely
ever make kistakes with this. :)
The task, is reading
lines of input, parsing them, and calling one of a set of functions with
the parsed arguments, based on what the arguments are.

Without investing too much time. This is
what I would start with:

struct functor {
virtual void operator()() const = 0;
};

typedef std::map<std::string,functor*> func_map;

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #9
"Hendrik Schober" <Sp******@gmx.de> wrote in news:eJVeN9D7DHA.2300
@TK2MSFTNGP10.phx.gbl:
Anonymoose <Ihatespam> wrote:
"Hendrik Schober" <Sp******@gmx.de> wrote in news:epnSyDx6DHA.2560
@TK2MSFTNGP09.phx.gbl:
> Mihajlo Cvetanovic <ma*@RnEeMtOsVeEt.co.yu> wrote:
> > [...] I believe there's a more elegant solution for your design,
> > but more background data is needed. What should this class do?
>
> It looks like Anonymoose is trying
> to re-invent virtual functions. When
> I saw the posting I wondered why one
> wouldn't use them for this task.


Cause he doesn't understand them that well (yet).


They do pretty much exactly what you are
trying to do. Only that compilers rarely
ever make kistakes with this. :)
The task, is reading
lines of input, parsing them, and calling one of a set of functions with
the parsed arguments, based on what the arguments are.

Without investing too much time. This is
what I would start with:

struct functor {
virtual void operator()() const = 0;
};

typedef std::map<std::string,functor*> func_map;


Thanks - that hurts my head, but I'll dig into it.
Nov 17 '05 #10
Anonymoose <Ihatespam> wrote:
[...]
Without investing too much time. This is
what I would start with:

struct functor {
virtual void operator()() const = 0;
};

typedef std::map<std::string,functor*> func_map;


Thanks - that hurts my head, but I'll dig into it.

To me fiddling with those nasty function
signatures make my brain spin. :)

Using functors instead of functions has
the great advantage that they can carry
around state:

struct my_functor : public functor {
my_functor(const std::string& str) : str_(str) {}
virtual void operator()() const
{
// ca use 'str_' here
}
std::string& str_;
};

void f(const functor& func);

void g()
{
f( my_functor("sn") );
f( my_functor("afu") );
}

With real function( pointer)s you need
global variables or -- worse -- 'void*'
in to do this.

When you do this with a map like the
above: There is the issue of life time
for those functors. If you create them
using 'new', you should use some smart
pointer ins the map, not a raw pointer.
In the boost lib (www.boost.org) is one
that fits your needs. (You must not use
'std::auto_ptr<>' for STL containers.)

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
17033
by: Grzegorz Staniak | last post by:
Hello, I'm a newbie Python user, a systems administrator - I've been trying to switch from Perl to Python for administrative tasks - and one thing I cannot understand so far is why I need the...
12
1910
by: Thomas Heller | last post by:
I once knew how to do it, but I cannot find or remember it anymore: How can I attach *class* methods to a type in C code, when I have a function PyObject *func(PyObject *type, PyObject *arg);...
0
1616
by: Maarten van Reeuwijk | last post by:
I am constructing a "placeholder array" to mimick Numeric arrays. I have a 3D regular structured domain, so I can describe the axis with 3 1D arrays x, y and z. All the variables defined on this...
0
1123
by: Heiko Wundram | last post by:
Hi all! I've written a user type, and I've declared class methods on it. Now... Strange things are'a happening, and the class methods which are declared on the type get the following output from...
4
7993
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
18
6931
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
10
1619
by: David Hirschfield | last post by:
Here's a strange concept that I don't really know how to implement, but I suspect can be implemented via descriptors or metaclasses somehow: I want a class that, when instantiated, only defines...
2
2910
by: jccorreu | last post by:
I'm taking in data from multiple files that represents complex numbers for charateristics of different elements. I begin with arrays of doubles and interpolate to get standard values in real and...
13
3023
by: noone | last post by:
consider the following problem: You have a C style library and API that uses callbacks to implement functionality. Examples of this are X11 API, OpenGL/GLUT...The List goes on. The power of...
1
933
by: megelton | last post by:
Hello, How does a new vb 2005 express "STUDENT" find which namespace - class methods are useful to solve a programming problem? For example, I have worked with StreamReader to open a text...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.