Pointers to Functions within instanced classes

Started by
7 comments, last by Genjix 19 years, 3 months ago

	A alpha;
	alpha.a = 10;

	Call((alpha.Func)());


Assuming Call takes a void function pointer as a parameter, does anyone know the syntax for this, with different class types?

Advertisement
Your post isn't very detailed, so I'm not completely sure what you are asking. Are you trying to pass a member function as an argument in C++? If so, you can't do that. The problem is that non-static member functions work differently than regular functions, so they are not supported by C++ function pointers.

In Managed C++, this can be done with delegates.
Take a look at Boost's Boost.Function library. It might do what you want.
Quote:Original post by nimrand
The problem is that non-static member functions work differently than regular functions, so they are not supported by C++ function pointers.


You can have method pointers in c++ to non-static class methods. They are declared like so:
  void ( classname::*mthdptr )() // no args, returns void
Using them is a little convoluted though:
classname c;void ( classname::*mthdptr )();mthdptr = classname::test; // assign method to pointer( c.*mthdptr )(); // call method


You're probably better off implementing a class that stores a pointer to the class and calls the appropriate method. I don't know Boost, but jdhardy's suggestion seems to be exactly that. [edit] Err, actually Boost's website mentions argument binding, and gives several possibilities here.

Regards,
jflanglois

[edit] Genjix, for what you are trying to do you may want to look at the section on callbacks at function-pointer.org

Ok, thanks.


i cant get it to work with different object types though :/, how can i do this?


the reason i ask is that the current method i use involves inheriting an abstract base class, but since all this base class consists of is one function and 3 overloaded functions, i want to cut it out and replace with a function with 3 function pointer arguments... :D


Thanks again.

I'm not sure I understand exactly what it is you're trying to do.
Could you give some code?
Quote:Original post by Genjix

Ok, thanks.


i cant get it to work with different object types though :/, how can i do this?


the reason i ask is that the current method i use involves inheriting an abstract base class, but since all this base class consists of is one function and 3 overloaded functions, i want to cut it out and replace with a function with 3 function pointer arguments... :D


Thanks again.

I think you could do that with templates(untested):
template<class Class>void call(void (Class::*method)(), Class object) {  (object.*method())();}

or, an even more flexible function(again, untested):
template<class Class, typename Arguments>void call(void (Class::*method)(Arguments), Class object, Arguments args) {  (object.*method(Arguments))(args);}
Easier with templates:
class Alpha{  //...  void func(void);  //...};template <typename T>Call(T & t){  t.func();}
Simply ensure that each class provides func().

Thanks for the solution, didnt think oof templates :P.


jflanglois : what i WAS doing was every class that used the function, inherited a base class with the function as a member, and the functions that would be passed in here were abstracted (meant to be overriden, and any calls to them were meant to be to the parent class).


This topic is closed to new replies.

Advertisement