pointer to a member function of a class - must specify class?

M

Matthew

I have a function pointer as a data member of a class MenuItem. I'd
like to be able to later set this function pointer to point to lots of
different functions, including member functions of other classes. I'm
going to only point to functions which take no arguments and have
return type void, but I don't want to be limited to pointing to member
functions of only one class. This would give me something like an
object oriented switch statement.

As I read more, I'm getting the sense that I can't have this much
freedom. It's not enough to restrict myself to just functions that
take no arguments and return void. If I'm going to be pointing to
member functions of a class, I have to specify the class.

Is this understanding of the limitiations of function pointers
correct?

Thank you for your time!
 
P

Puppet_Sock

I have a function pointer as a data member of a class MenuItem. I'd
like to be able to later set this function pointer to point to lots of
different functions, including member functions of other classes. I'm
going to only point to functions which take no arguments and have
return type void, but I don't want to be limited to pointing to member
functions of only one class. This would give me something like an
object oriented switch statement.

As I read more, I'm getting the sense that I can't have this much
freedom. It's not enough to restrict myself to just functions that
take no arguments and return void. If I'm going to be pointing to
member functions of a class, I have to specify the class.

Is this understanding of the limitiations of function pointers
correct?

Yep. And before you can actually call that function you
need an instance of the class to work on.

Though, if you are careful, you can get some of what you
want through polymorphism. Maybe.

It *sounds* like you are trying to do callbacks. There is
a discussion of callbacks in the FAQ.
Socks
 
D

Daniel Pitts

Matthew said:
I have a function pointer as a data member of a class MenuItem. I'd
like to be able to later set this function pointer to point to lots of
different functions, including member functions of other classes. I'm
going to only point to functions which take no arguments and have
return type void, but I don't want to be limited to pointing to member
functions of only one class. This would give me something like an
object oriented switch statement.

As I read more, I'm getting the sense that I can't have this much
freedom. It's not enough to restrict myself to just functions that
take no arguments and return void. If I'm going to be pointing to
member functions of a class, I have to specify the class.

Is this understanding of the limitiations of function pointers
correct?

Thank you for your time!
Look into TR1 function template.

<http://www.devx.com/cplus/10MinuteSolution/32455> wrote:
"The recently standardized std::tr1::function class template is a
generic callback mechanism with intuitive and uniform syntax and
semantics. This generalized abstraction mechanism encapsulates any
callable entity—regardless of its underlying implementation. Learn how
to use this class to simplify your code and expand your design choices."
 
M

Matthew

Thank you, Socks and Daniel. That does appear to be what I need.

Basically what I want is a system for linking menu options to submenus
(which will return to the calling menu when exiting the submenu) and
also linking menu (and submenu) options to various bits of programming
functionality (which then return to the menu or submenu from whence
they came). Embedding switch-within-switch would work, but it would be
messy. Also, I think that relying on non-class functions and pointers
to those functions could give me some 'subroutine logic'. Each menu
would be a function. The purpose of each function would be to continue
program flow to some new function within the program. Exiting a
submenu would just be exiting that menu's function. In that case,
program control would return to the calling function (menu). My C++
textbook has a demonstration of how to use an array of function
pointers to implement a menu. The example only covers using a single
menu to select one function from an array of three function pointers.
The entire bit of code was not in a class, just in main(). My initial
thought was to encapsulate each menu in a function and then menus
could lead to new menus by pointing to the appropriate function.
Control could be turned over to non-menu functionality by invoking the
member functions of 'handler' objects which would embody the program's
features. Once those functions terminated, return would control to the
menu which called them. This line of thought led to my investigation
of function-pointers. I also thought about making the menus themselves
into objects (not just the program feature handlers) in order to
simplify the whole framework. With objects, I could just specify the
links without having to recode arrays of pointers at each new menu or
recode selection logic to choose an element of the array.

The problem tackled by Daniel's link appears to be exactly my problem:
"How do you generalize the concept of callbacks while maintaining
similar syntax and semantics to function pointers?"

I will also read the faq section on callbacks (which seems to be
section 33 on function pointers, assuming I'm using the right faq at
http://www.parashift.com/c++-faq-lite/).

Thank you again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top