Skip to main content
gamedev.net
🔒 Locked

Confusing C++ member function pointer problem

Started by Motorherp Dec 17, 2004 at 2:40 PM 5 replies 950+ views
Original Post
Motorherp
Motorherp
Consider the following if you will:

ClassA
{
	int	func1(bool(*pFunc)(float));
};


ClassB
{
	void	func2(ClassA& classAInstance);
	bool	func3(float number);
};

void ClassB::func2(ClassA& classAInstance)
{
	classAInstance.func1( 'insert pointer to this ClassB instance's func3 member function' );
}

Anyone know how I get that function pointer in there without making my functions static??? I've read some member function pointer tutorials on the net already but I've not been able to get anything to work yet. Help.....
[size="1"] [size="4"]:: SHMUP-DEV ::
SiCrane
SiCrane
You need to change ClassA::func1() to

int func1(bool (ClassB::*pFunc)(float));


Of course, I'd throw in some typedefs in there to clean things up a bit.
Motorherp
Motorherp
So there's no way to keep this general, I have to tie this down to specific class definitions???
[size="1"] [size="4"]:: SHMUP-DEV ::
The Rug
The Rug
Quote:
Original post by Motorherp
So there's no way to keep this general, I have to tie this down to specific class definitions???


Yeah, basically. You might want to check this out, it helped me.
the rug - funpowered.com
Motorherp
Motorherp
Cheers for the help everyone.....muchly appreciated. For those interested I thought I'd post the problem solved since this stuff is really picky about syntax so I doubt I'm the only one who's struggled with this:

class ClassB;class ClassA{	int	func1(ClassB& instance, bool(ClassB::*pFunc)(float));};int ClassA::func1(ClassB& instance, bool(ClassB::*pFunc)(float)){	float someNumber = 1.0f;	(instance.*pFunc)(someNumber);		// NB: Don't forget the brackets around instance.*pFunc}class ClassB{	void	func2(ClassA& classAInstance);	bool	func3(float number);};void ClassB::func2(ClassA& classAInstance){	classAInstance.func1(*this, &ClassB::func3);}
[size="1"] [size="4"]:: SHMUP-DEV ::
Sharlin
Sharlin
As the old saying goes, everything can be solved by adding an (or a couple of) extra layer of indirection. Enter boost::function, a completely generic function pointer abstraction. Eats just about anything you throw at it: global functions, methods, function objects, as long as the signature matches. Also check out boost::bind while you're at it.

** EDIT ** Some examples:

#include <boost/function.hpp>using std::cout;struct foo{  void g() { cout << "foo::f\n"; }  // simulate the implicit "this" parameter  static void h(foo *self) { cout << "foo::g\n"; }};struct bar{  void operator () (foo *f) { cout << "bar::operator()\n"; }};void baz(foo *f) { cout << "baz\n"; }int main(){  foo f;  boost::function<void (foo *)> fn;  fn = &foo::g; fn(&f);  fn = &foo::h; fn(&f);  fn = bar();   fn(&f);  fn = baz;     fn(&f);}


#include <boost/function>#include <algorithm>class foo { int f(int i) { return 2*i; } };class bar { int operator () (int i) { return 3*i; } };int baz(int i) { return 4*i; }typedef boost::function<int (int)> func;int main(){  foo f;  bar b;  func fn;  fn = std::bind1nd(&foo::f, &f)    // bind the implicit "this" parameter  std::cout << fn(5) << '\n';       // prints '10'  fn = b;  std::cout << fn(5) << '\n';       // prints '15'  fn = baz;  std::cout << fn(5) << '\n';       // prints '20'}


[Edited by - Sharlin on December 17, 2004 4:40:28 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.