Callbacks

What is the use of callback exactly in C/C++

When I was learning API(glfw) there had been told about that many times.. What exactly it is?
or in simpler terms its just a function that the engine can call.

i took the following example from http://www.glfw.org/docs/latest/quick.html


this is a function for handling errors, I have no intention of calling it myself, it is a custom error handler for glfw to call when things go wrong.

1
2
3
4
void error_callback(int error, const char* description)
{
    fputs(description, stderr);
}


I can then tell glfw to call that function whenever there is an error.

glfwSetErrorCallback(error_callback);

you can name the function and its parameters anything you like but the return type and parameter types must be the same as the api specifies, only a function pointer is passed to glfw.

you can change the handler as often as you like.

1
2
3
4
5
6
7
8
9
10
void startupErrorHandler(int error, const char* description)
{
  // do something about the startup error
}

void renderingErrorHandler(int error, const char* description)
{
  // do something about the rendering  error
}


1
2
3
4
5
6
7
glfwSetErrorCallback(startupErrorHandler);

// do the startup stuff.

glfwSetErrorCallback(renderingErrorHandler);

// do the rendering 

Thanx a lot
Topic archived. No new replies allowed.