Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

TrigonLoki

Help with function pointers

This topic is 6925 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

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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
I made dedicated status! Yippee! Milestones! (Even though I really shouldn''t care about such vain things )

I like food.

Share this post


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

Share this post


Link to post
Share on other sites
As far as i can tell, taking out the parentheses is just like typing

673;

which is a legal statement.

Trigon

I like food.

Share this post


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!