Help with function pointers

Started by
6 comments, last by TrigonLoki 24 years ago
Okie dokie. Hi everyone! I need help using function pointers inside of classes. I have a class, called CGraph, that has two functions in it for building colors, BuildColor15 and BuildColor16 (each of which takes three UCHAR's). I decided that instead of having switch statements all over the place, I'd make a function pointer inside the CGraph class called fnBuildColor. Could someone please show me the correct syntax for declaring, assinging, and using them? And could you write my entire game for me? Seriously, though, I've been trying for quite a while to get it right. The code I have currently looks like this: Declaration (in the public section of CGraph):
	
USHORT (CGraph::*fnBuildColor) (BYTE, BYTE, BYTE);	// Function pointer to the build color functions
  
Assignment (In the constuctor of CGraph):

CGraph::fnBuildColor = BuildColor15;
  
Utilisation (In WinMain(), with all the right headers):

CGraph* Screen;
Screen = new CGraph(640,480);
USHORT blah = Screen->fnBuildColor(5,7,8);
  
The utilisation code gives me an 'undeclared identifier' error, but the compiler isn't complaining about other things. I remember that I somehow made the 'indeclared udentifier' error go away with some different declaration syntax, and then got a 'term does not evaluate to a function' error. Please help, I want to try to make this work. I could probably work around using function pointers at all in this problem, but I want to see how they work and everything, so this seems like a good opportunity. -Edit- Made a little typo which might have made you assume soemthing totally obvious was wrong, but nope, the problem's still there - Trigon I like food. Edited by - TrigonLoki on 5/7/00 5:29:38 PM
I like food.
Advertisement
Hey man! I love food too.

To declare a pointer to any non-member function:

void Function(int&, int&);//prototype (probly in header file)
void Function2(int&, int&);
int main()
{
void (*pFunc) (int&, int&); //(*pFunc) MUST BE IN PARENTHESIS!
pFunc = Function;
pFunc(10,10);
pFunc = Function2;
pFunc(10,10);
return 0;
}

And to do member function pointers:

class Math
{
public:
int v1;//to do op.s on
int v2;
int Add();
int Multiply();
};
int main()
{
int (Math::*pFunc)();
Math junkmath;
junkmath.v1 = 2;
junkmath.v2 = 4;
pFunc = Math::Add;
cout<pFunc = Math::Multiply;
cout<return 0;
}

I'm pretty sure that's right. I just wrote it on the spot, but while looking at a reference manual (I only gave you pseudocode to save space). If you need anything else, just give me a yell.

~BenDilts( void );

Edited by - BeanDog on 5/7/00 5:52:19 PM
Thanks for the reply. (I liked your little BeanDog shooting game!)

But, that didn't fix the problem. My fptr is a member of the same class as the function I'm trying to get it to point to. I made a little test app to try and demonstrate the problem.

class Dumb{public:	Dumb();	~Dumb();	int Func();	int (Dumb::*fnPFunc)();};int Dumb::Func(){	cout << "Lala!\n";	return 5;}Dumb::Dumb(){	cout << "Constructing\n";	fnPFunc = Func;}Dumb::~Dumb(){	cout << "Destroying\n";}Dumb* dum;int main(int argc, char* argv[]){	dum = new Dumb;	dum->fnPFunc(); //<---- Gives term does not evaluate to a function error	delete dum;	return 0;}  


(If that gets messed up by the board, just click the little Edit Message button atop my post and look at it there.)

It only gives that one error, but I fear I'm going about it the wrong way altogether.

- Edit: Another typo (I didn't get enough sleep!) -
Trigon




I like food.

Edited by - TrigonLoki on 5/7/00 6:13:00 PM
I like food.
I made dedicated status! Yippee! Milestones! (Even though I really shouldn''t care about such vain things )

I like food.
I like food.
To take away the error, don''t put parentheses after the function when it is being called (on line 28, "dum->fnPFunc" instead of "dum->fnPFunc()") . This does not solve the main problem, but it might help.
As far as i can tell, taking out the parentheses is just like typing
673; 

which is a legal statement.

Trigon

I like food.
I like food.
You need to make the call like:

(dum->*(dum->fnPFunc))();

The first dum is the object that the function pointer will be called on. The ->* says call a pointer to a member function using the previous object. the (dum->fnPFunc) actually resolves to the function pointer to be called.

That explanation is probably as clear as mud. Note that
(dum->*dum->fnPFunc)() will work, but
dum->*dum->fnPFunc() won''t work. (Has to do with operator precendance.)
Wow. I wouldn''t have guessed THAT in a million years. Thanks bunches!


Trigon


I like food.
I like food.

This topic is closed to new replies.

Advertisement