Sign in to follow this  

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

This topic is 4685 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[i] = 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
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[i] = 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[i] && ptrfKeyFunction[i]!=NULL)
{
(ptrfKeyFunction[i])(); //*ERROR*
}
}
}


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

Share this post


Link to post
Share on other sites
Quote:
Original post by sphinx23
Yes, my previous suggestion was not quite right ;) Try this:


(this->*ptrfKeyFunction[i])();


That should (hopefully) do the trick :)



Yes now it does work.
Thank you very much.

Share this post


Link to post
Share on other sites

This topic is 4685 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this