Jump to content
  • Advertisement
Sign in to follow this  
Genjix

Pointers to Functions within instanced classes

This topic is 5010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

	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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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);
}



Share this post


Link to post
Share on other sites
Easier with templates:

class Alpha
{
//...
void func(void);
//...
};

template <typename T>
Call(T & t)
{
t.func();
}

Simply ensure that each class provides func().

Share this post


Link to post
Share on other sites

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).


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!