[C++] Indirect Function Call Within a Function

Started by
4 comments, last by wioneo 13 years, 9 months ago
I want to condense my controller object looping through subordinate objects into one function for rendering, updates, etc, so I looked up function pointers, and I have a VERY loose understanding of how this works but I attempted to sue this...
void c_control::call_unit_function(void (*unit_function)()){	for (int i=0; i<sizeof(unit_tlist)/sizeof(*unit_tlist); i++)	{		if (unit_tlist==0){continue;}		unit_tlist->unit_function();	}}

...to pass a subordinate class unit(pointers stored in the array) function to all living units from the controller.

For example...
class unit{public:	void draw_unit(){/*draw*/}};class controller{	void loop_units(unit_function)	{                for (/*loop*/)                {		       current_unit->unit_function();                }	}	void draw_units()	{		loop_units(unit::draw_unit);	}}

I hope that that explains what I am trying to do...
Advertisement
1) The standard library already gives you a way to do this kind of "generic looping". It is called std::for_each().

2) Your problem is that pointers to member functions are not the same thing as pointers to functions. This is a very complex topic and you must read and understand all of this if you hope to get anywhere.
Looks like what you're looking for is a pointer to a member function...

It's not something I actually use often, so I can't personally give you a great deal of information on how to use them. The updated code might look something like this...

#include <iostream>#include <vector>#include <string>class unit{   std::string _name;public:   unit(std::string name) { _name = name; }   void draw_unit() { std::cout << _name << std::endl; }};class controller{   std::vector<unit> _units;   void loop_units(void (unit::*unit_function)())   {      for (int i = 0; i < _units.size(); i++)         (_units.*unit_function)();   } public:   void draw_units()   {      loop_units(&unit::draw_unit);   }   controller()   {      _units.push_back(unit("Bob"));      _units.push_back(unit("Steve"));      _units.push_back(unit("Mike"));   }}; //end classint main() {   controller c;   c.draw_units();   system("pause");}

Google can probably give you more information.

I believe this is the site I referenced last time I was looking at pointers to member functions.

Hope that helps.
Thank you for the suggestion and link, I will start going through that.
Quote:Original post by Zahlman
2) Your problem is that pointers to member functions are not the same thing as pointers to functions. This is a very complex topic and you must read and understand all of this if you hope to get anywhere.
While it is essential to understand all of that in the end, for the short term you might just want to learn how to use boost::function and boost::bind, and be done with it....

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

I read a few pages from that poster, thank you for the link, I've got it working now. Thank you for the boost pointers suggestion, swiftcoder.

This topic is closed to new replies.

Advertisement