Classes and function pointers

Hey,

What is the correct way of doing this? I don't want to change the structure... I just need to know the correct syntax. Been reading online but its just a bit confusing... can you help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

class fruit {

public:
	bool (*eat)(int bitesize);
};

class apple: public fruit {

};

class worm {

public:
	apple dinner;

	bool nibble(int bitesize) {
		return true;
	};

	void dosomething() {
		dinner.eat = nibble; //error

		dinner.eat(1);
	};

};

int main() {
	worm temp;
	temp.dosomething();
};



Cheers!
Last edited on
The problem is that worm::nibble requires an object to act as the 'this' pointer.

There are several ways to address this problem.

1) Make nibble static so you don't need an object (this of course has the sideeffect of it not being able to use class members in that function):

1
2
3
4
5
6
7
class worm {
//...
	static bool nibble(int bitesize) {
		return true;
	};
//...
	dinner.eat = &worm::nibble;


2) Bind the function and give it the object:

1
2
3
4
5
6
7
8
9
10
#include <functional>  // C++11, your compiler probably supports it.  If not, upgrade.

class fruit {
public:
    std::function<bool(int)> eat;
};

class worm {
//...
    dinner.eat = std::bind( &worm::nibble, std::ref(*this), std::placeholders::_1 );



3) Use "traditional" C++ function pointers (RESTRICTION: only works if 'worm' is the only class that will be eating fruit)

The syntax for this is really funky and hard to remember so I won't post an example unless you're really interested. But given the restriction, I doubt it will be what you need.
Thanks...

The first example of this problem in my code can be resolved with static, but the second cannot.... and I CAN'T upgrade.


Is it possible for you to possibly point me in the right direction for the third approach, please?
Last edited on
This is the latest version...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class engine { //ctrl
public:
	bool (*pf)(int i);
};

class f {
public:

	virtual void setFormDefaults() {};
};

class theform: public f {
public:
	engine e;

	bool h(int i) {
		return true;
	};

	void setFormDefaults() {
		e.pf = h; //problem line
	};
};

int main() {
	theform temp;
	temp.setFormDefaults();
};


I cant make h() static :(
options 2 and 3 are still available. I don't know why you "can't"... you have an internet connection, don't you? ;P Or are you forced to work with someone else's toolset (like for a job or something)


Anyway, Option 3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class theform;

class engine{
public:
  bool (theform::*pf)(int i);
  theform* obj;

  // called with:
  //   (obj->*pf)( xx );
};

class theform: public f {
//...
  void setFormDefaults() {
    e.pf = &theform::h;
    e.obj = this;
//... 



As you can see, this approach is cumbersome because you have to keep track of that object pointer, and it only works for the 'theform' class. If you have a different function in a different class that you want to point to, this won't work.
};
Ummmmm. Annoyingly, none of these 3 solutions will work with what I currently have.

I don't suppose there is a 4th way? :)

1) Can't make it static
2) Can't add the header file
3) It won't be the only class using the class

I appreciate your help on this!
Last edited on
It seems the first solution is probably the one I should be looking at implementing.

That requires me to solve the problem of my static functions (to be passed) containing non static members.

I suppose my efforts should be concentrated on that instead.
Sorted it! Solved the static member problem and went with resolution 1.

Thanks a bunch. Your help is really appreciated.
Topic archived. No new replies allowed.