Jump to content
  • Advertisement
Sign in to follow this  
md_lasalle

Passing a function adress as a parameter (c++)

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

Considering this code... in cConsole.h:
public:

void	InitKeys();
void	BindKey(BYTE keyID, void(*keyFonction)());
in cConsole.cpp
void cConsole::InitKeys()
{
	
  //initialize every fonction pointers in the array to NULL
  for(BYTE i=0;i<255;i++)
  {
    ptrfKeyFonction = 0;
  }

  //Setting keys we know about
  BindKey(27,&(cConsole::Stop));     //ESCAPE KEY stops everything *ERROR*
 
}

void cConsole::Stop()
{
  done = true;  // STOPS the mainloop from continuing
}

void cConsole::BindKey(BYTE keyID, void(*Fonction)())
{
	ptrfKeyFonction[keyID] = Fonction;
}

How could i get my call to BindKey to work WITHOUT using anything static? Thank you and if you need more precision feel free to ask. [Edited by - Oluseyi on February 11, 2005 2:33:45 PM]

Share this post


Link to post
Share on other sites
Advertisement
Also check out the Fast Delegate library, which allows you to freely intermix static function pointers and member function pointers with no run-time cost. I use it all the time for purposes like yours. Brilliantly useful.

Share this post


Link to post
Share on other sites
You need to make a few changes. In cConsole.h:

typedef void (cConsole::*LPKeyFunction)(); // This makes life easier

public:
void InitKeys();
void BindKey(BYTE keyID, LPKeyFunction pfnFunction);

private:
LPKeyFunction ptrfKeyFunction[256];




In cConsole.cpp:

void cConsole::InitKeys()
{
//initialize every function pointers in the array to NULL
for(BYTE i=0;i<255;i++)
{
ptrfKeyFunction = 0;
}

//Setting keys we know about
BindKey(27,&(cConsole::Stop)); //ESCAPE KEY stops everything *ERROR*
}

void cConsole::Stop()
{
done = true; // STOPS the mainloop from continuing
}

void cConsole::BindKey(BYTE keyID, LPKeyFunction pfnFunction)
{
ptrfKeyFunction[keyID] = pfnFunction;
}




That aught to fix it.

[Edited by - Evil Steve on February 11, 2005 8:58:28 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Erzengeldeslichtes
Shouldn't "typedef void (cConsole::LPKeyFunction)(); " be "typedef void (cConsole::*LPKeyFunction)(); "?
Yup, typo :). I edited my post to include the correction.

Share this post


Link to post
Share on other sites
ok just one little problem now with the call of that function


void cConsole::checkKeys()
{
for(BYTE i=0;i<255;i++)
{
if (touche && ptrfKeyFunction!=NULL)
{
(ptrfKeyFunction)(); //*ERROR*
}
}
}


it says : "error C2064: term does not evaluate to a function"

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!