473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Function Pointers To Member Functions

I know you can do:

int (MyClass::*ptr)(int) = &MyClass::func;
cout << (myObj.*ptr)(5);

to point to a member function of class MyClass. But how would you go about
making this generic (i.e., not restricted to any specific class, like
MyClass)? You can probably resort to C function pointers, but I'd rather
not. Thanks in advance.
Jul 22 '05 #1
7 1287
"OvErboRed" <pu******@SPAMoverbored.net> wrote...
I know you can do:

int (MyClass::*ptr)(int) = &MyClass::func;
cout << (myObj.*ptr)(5);

to point to a member function of class MyClass. But how would you go about
making this generic (i.e., not restricted to any specific class, like
MyClass)? You can probably resort to C function pointers, but I'd rather
not. Thanks in advance.


You cannot make it generic. Pointers to members are class-specific.
Period.

Victor
Jul 22 '05 #2
Fine, but is there no other way to pass in member functions as general
callbacks? (I guess I'm basically trying to get the same effect as
runnables in Java or delegates in C#, but using function pointers instead
of classes implementing Runnable or IDelegate.)

"Victor Bazarov" <v.********@comAcast.net> wrote in
news:jhYic.35240$aQ6.1900352@attbi_s51:
You cannot make it generic. Pointers to members are class-specific.
Period.

Victor

Jul 22 '05 #3
"OvErboRed" <pu******@SPAMoverbored.net> wrote...
Fine, but is there no other way to pass in member functions as general
callbacks?
Usually not. See FAQ.
(I guess I'm basically trying to get the same effect as
runnables in Java or delegates in C#, but using function pointers instead
of classes implementing Runnable or IDelegate.)
My guess is that it's possible, but you'll be involving templates and
thus mostly giving up run-time choices.

"Victor Bazarov" <v.********@comAcast.net> wrote in
news:jhYic.35240$aQ6.1900352@attbi_s51:
You cannot make it generic. Pointers to members are class-specific.
Period.

Victor

Jul 22 '05 #4
"OvErboRed" <pu******@SPAMoverbored.net> wrote in message
news:Xn******************************@127.0.0.1...
I know you can do:

int (MyClass::*ptr)(int) = &MyClass::func;
cout << (myObj.*ptr)(5);

to point to a member function of class MyClass. But how would you go about
making this generic (i.e., not restricted to any specific class, like
MyClass)? You can probably resort to C function pointers, but I'd rather
not. Thanks in advance.


You can't. You can try cheating, if you don't mind writing non-C++,
non-portable code that might stop working on a different compiler, or the
next version of your current compiler, or might never work at all on certain
classes, depending on how the compiler implements them. I once had to work
on a large, complex and highly event-driven application. The compiler at the
time didn't have templates. I decided that the usefulness of such a generic
pointer outweighed language and portability issues, and I wrote some nasty
macros that enabled any class to have a member-function event handler. The
event manager thought it was always calling a member of class EventHandler,
which was defined as follows:

class EventHandler
{
};

In fact, it was calling member functions of a large number of different,
unrelated classes at different times from the same call statement.

DW

Jul 22 '05 #5
OvErboRed wrote:
I know you can do:

int (MyClass::*ptr)(int) = &MyClass::func;
cout << (myObj.*ptr)(5);

to point to a member function of class MyClass. But how would you
go about making this generic (i.e., not restricted to any specific
class, like MyClass)? You can probably resort to C function
pointers, but I'd rather not. Thanks in advance.


If this is a way, use a base class which all classes inherit of.
Virtual methods will grant you access to the methods of the derived
classes, too.

Bernhard
Jul 22 '05 #6
> "Victor Bazarov" <v.********@comAcast.net> wrote in
news:jhYic.35240$aQ6.1900352@attbi_s51:
You cannot make it generic. Pointers to members are class-specific.
Period.

Victor

NB: OvErboRed, please do not top-post (I moved your reply down).
"OvErboRed" <pu******@SPAMoverbored.net> wrote in message
news:Xn******************************@127.0.0.1... Fine, but is there no other way to pass in member functions as general
callbacks? (I guess I'm basically trying to get the same effect as
runnables in Java or delegates in C#, but using function pointers instead
of classes implementing Runnable or IDelegate.)


Such a 'generalized callback' can be implemented as a library in C++.
boost.org, a peer-reviewed collection of C++ libraries, provides
libraries that do what you are asking for, but it is somewhat complex
and spread out among multiple compoents. The result, however, is
relatively simple -- as demonstrated by the example at:
http://www.boost.org/libs/bind/bind....boost_function
See the doc of boost::bind and boost::function for more info.
http://www.boost.org/doc/html/function.html

Note that these utilities are likely to be integrated into the
next revision of the C++ standard, and are even already supported
by some compiler vendor(s) as documented in this technical report:
http://anubis.dkuug.dk/jtc1/sc22/wg2...2003/n1540.pdf
Also of potential interest is the related boost signals library:
http://www.boost.org/doc/html/signals.html

hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 22 '05 #7
OvErboRed <pu******@SPAMoverbored.net> wrote in message news:<Xn******************************@127.0.0.1>. ..
I know you can do:

int (MyClass::*ptr)(int) = &MyClass::func;
cout << (myObj.*ptr)(5);

to point to a member function of class MyClass. But how would you go about
making this generic (i.e., not restricted to any specific class, like
MyClass)? You can probably resort to C function pointers, but I'd rather
not. Thanks in advance.


No. The MyClass part, which maps to the 'this' pointer in the method,
behaves like a hidden first argument. Therefore, you can't have a single
pointer type, just as there is no single function pointer type for both
'void foo(MyClass&)' and 'void bar(YourClass&)'.

Besides, how would you invoke it? If you have such a generic pointer,
and you initialize it with &MyClass::foo, and I invoke it on
std::string s;, what happens?

That said, you could use a union of funtion pointers.
e.g.
union Foo_or_Bar_MP {
void (Foo::*fooMethod)();
void (Bar::*barMethod)();
};
You must ensure that fooMethod is initialized when you
call pMyFoo->*(method.fooMethod). How you do that is up to you,
but it is legal and will work everywhere.

Regards,
Michiel Salters
Jul 22 '05 #8

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

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
5
by: xuatla | last post by:
Hi, I have the following code for function pointer. compiling is ok. Can you help me to check whether it's a good way to implement as: class CA { ..... private: void f1( double ) ;
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
3
by: Bilgehan.Balban | last post by:
Hi, How do I declare an array of function pointers and assign each element of the array with public member functions of a class? Is it possible that the array is not a member of the class? ...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
2
by: Amadeus W. M. | last post by:
I have a bunch of templated functions: template <class Type_t> double f2(Type_t x) { return 2*x; } template <class Type_t> double f3(Type_t x) { return 3*x; }
1
by: pheres | last post by:
Hi, I'm trying to pass pointers to member functions around in my code. The test with pointers to non-member function works fine: Code: void callOut( void (*callback)() ) { callback();
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.