Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

C++, How to make function run another function, eg void func(void func2)

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

Hi, I want to run a function from a function. eg.
//init
void init(void run)  {
    run();
}

//and being used...
void run() {
}

int main() {
    init(run());
    return 0;
}

thanks

Share this post


Link to post
Share on other sites
Advertisement
You have a problem. Functions can take values as parameters, but void is not a value type (except void *). Furthermore, functions are not values, so you can't pass them - at least, not directly.

Solution? Use a function pointer.

[Edit: source snippet removed.]

Share this post


Link to post
Share on other sites
Ok, I made my function ok, eg void run(void func()){func();}

But is it possible to store it in a global variable.

eg. something like so:

void globalFunc();

void doBeep() {
Beep(33,33);
}


int main() {
globalFunc = doBeep;

globalFunc(); //goes beep
return 0;
}



Share this post


Link to post
Share on other sites
ok, first, it's:
void run(void (*func)()){func();}
not:
void run(void func()){func();}

and to answer your question, the way you make a function pointer is

void (*func)();

Share this post


Link to post
Share on other sites
Quote:
Original post by Roboguy
ok, first, it's:
void run(void (*func)()){func();}
not:
void run(void func()){func();}

and to answer your question, the way you make a function pointer is

void (*func)();


Thanks it works, but out of curiosity i was wondering what the difference between:

void run(void (*func)()) {
func();
}
//and
void run(void func()) {
func();
}


is?


And when I'm using the global function pointer, eg

void (*func)();
void doBeep() {
Beep(200,50);
}

void run(void (*func)()) {
func();
}

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
func = doBeep; //do i do this or
func = &doBeep; // as they both work.

run(func);

return 0;
}

Share this post


Link to post
Share on other sites
One can't pass a function by value, it needs to be a function pointer [though the [std?] notation would suggest otherwise].

Share this post


Link to post
Share on other sites
func = &doBeep fits better with standard notation, but func = doBeep seems to be standard aswell, so why not?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"1) The second one doesn't work"

Nope, that works perfectly. It's a completely valid syntax.

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.

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!