Sign in to follow this  
spree

pointers to functions and Classes HELP!@#

Recommended Posts

Hey all, I want to be able to send a member function of a class as a parameter to another function of another class. When I send a normal function, one that does not belongs to a class, to A.set(p) it works great. But when I attempt to do the same with a function which belongs to a class it does not work, see class B. this is the error I get. C2664: 'A::Set' : cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void)' I understand that there is a difference between the pointers to functions which belong to classes and normal functions. Is there any cast or a way to by pass it?
#include "stdafx.h"
#include <iostream>
using namespace std;

class A
{
public:

	(void) (*f)(void);

	void Set(void (*_f)(void))
	{
		f=_f;

		f();//call f;

	}

};

class B
{
public:
	void print(void)
	{cout<<"Hello";}

	B()
	{
		A a;
		a.Set(print);// <<DOESNT WORK

	}


};

void p(void)
{
	cout<<"p";
}

int _tmain(int argc, _TCHAR* argv[])
{
	A a;
	a.Set(p);//<< WORKS GREAT
	return 0;
}

Share this post


Link to post
Share on other sites
As the FAQs said, Member function pointers cannot be converted to function pointers unless they are a static member of the class.

Furthermore, to take the address of member functions, the & operator is mandatory (Unlike in normal get-address-of-function code).


class B
{
public:
static void print(void)
{
cout<<"Hello";
}

B()
{
A a;
a.Set(&B::print);

}


};



If you want to use a member of B, consider using the boost::function and <http://www.boost.org/doc/html/function.htmlboost::bind
boost::mem_fn
boost::bind
libraries.

Share this post


Link to post
Share on other sites
If another class will be calling the function, then you do one of two things:

1) Make the Function public
2) Make the Other Class a friend class.

Anything else is too complicated and unreadable. You shouldn't have to deal with stuff like that.

Share this post


Link to post
Share on other sites
I recall having a similer problem a few months ago, where I needed to use a custom error handler in the spidermonkey javascript engine. I got into a nice long discussion with a friend about C and C++. We did come up with a possible solution that would allow pointers to member functions of a class, but its so -incredibly- specific to your entire dev environment, and very difficult to read, that its really not worth the time or energy. It involves nasty things like pointers to the vftable of classes. Bad bad bad. Dont do this.

In the end, I'm just using a static member function, which doesn't do exactly what I want, but its good enough for now.

Share this post


Link to post
Share on other sites
Thanks for the help guy.
I'll work something with the static members :)
Eventhough someone should put a fix into c++ .

And if you wanted to know, why I"m using pointers to member functions .
I have CollisionDetectors, each collision detector object,On collision , trigers an apropriate function which handles the collision.

So a game entity can have several collision detectors assosiated with certian functions which handle these collisions.

Share this post


Link to post
Share on other sites
Quote:
Even though someone should fix c++


truer words have never been spoken, my friend.

Fortunately, someone else thought the exact same thing and decided to fix it. The Fast Delegate library by Don Clugston provides a way of declaring function pointers and allowing either member functions or static functions to be assigned.

For the curious, the article also contains all the bizarre details of member function pointers and their wildly different implementations on different compilers.

Share this post


Link to post
Share on other sites
Quote:
Original post by ajas95
Quote:
Even though someone should fix c++


truer words have never been spoken, my friend.

Fortunately, someone else thought the exact same thing and decided to fix it. The Fast Delegate library by Don Clugston provides a way of declaring function pointers and allowing either member functions or static functions to be assigned.

For the curious, the article also contains all the bizarre details of member function pointers and their wildly different implementations on different compilers.


I went reading that page you linked, and book marked it for later use. Or at least tried to, turns out I've been there before, thought it looked familier. Good link though! ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this