[C++] Callback functions in C++

8060f823d3b004e7f088076ae8a159c7
0
TheJjokerR 101 Mar 12, 2011 at 15:53

Hey there,

I come here before you with nothing but HTML, PHP, CSS and Lua knowledge. Seeing how I consider myself skilled enough with Lua, I keep trying to do things in C++, that I could do in Lua.

Today I wanted to write some simple functions to detect which key is pressed, if a key is pressed, etc. All really basic and mostly useless seeing how most of these functions already exist.

Now I wanted to make some sort of function that works like this:

void keyDownCallback(int keyCode, function callbackFunc)
{
    while(true)
    {
        if(keyIsDown(keyCode))
        {
            callbackFunc(keyCode);
        }
    }
}

I hope you instantly see what I mean, I’m trying to call a function that is supposed to be hooked at a certain key press.

How would I go about doing this? Making the callbackFunc actually work?

I hope I can get some help from you guys here.

PS: You’re talking to a massive C++ noob here, so be careful not to make it too hard for me ;P

4 Replies

Please log in or register to post a reply.

A77e71b962cd6c7c3b885f0488452f1f
0
tobeythorn 101 Mar 12, 2011 at 16:35

Enjoy…

#include <stdio.h>
#include <stdlib.h>

typedef void (*function_ptr)(int);

void aFunction(int anInt)
{
    printf("goodbye world");
}

void keyDownCallback(int keyCode, function_ptr callbackFunc)
{
    while(1)
    {
        if(keyCode == 5)
        {
            (*callbackFunc)(keyCode);
        }
    }
}

int main()
{
    function_ptr myFunctionPointer = (function_ptr) &aFunction;
    keyDownCallback(5, myFunctionPointer);
    return 0;
}

Edit: Google is your friend: http://www.newty.de/fpt/fpt.html#callconv

8060f823d3b004e7f088076ae8a159c7
0
TheJjokerR 101 Mar 13, 2011 at 14:14

Ah thanks a lot for that! I actually did find that page with google, but your reply made me understand that page a whole lot more than I did before.

Anyways I have 1 more question: How much faster is while(1) compared to while(true), does it matter?

A77e71b962cd6c7c3b885f0488452f1f
0
tobeythorn 101 Mar 13, 2011 at 15:32

Glad you found the example helpful.

In c++, false is defined as 0 and true is defined as 1. In c (which is the compiler I used), there are no built in keywords “true” and “false”, so I just used while(1). There should be no difference, so go with the more expressive and human readable option: “true” and “false”.

Note too that I used the c function printf(…) but you should use the c++ method call:
cout << “goodbye world”;
The notation is weird, but should be a safer function to use. Essentially, its a macro way of saying something like:
cout.outputSomething(“goodbye world”);
cout is an instance of an ostream object that is hooked up to the console.

340bf64ac6abda6e40f7e860279823cb
0
_oisyn 101 Mar 14, 2011 at 15:21

You could also use an interface@tobeythorn

(*callbackFunc)(keyCode);

Note: you don’t have to dereference a pointer to a function before calling it. Doing simply “callbackFunc(keyCode)” will be fine. Also, better not use an explicit cast:

// instead of
    function_ptr myFunctionPointer = (function_ptr) &aFunction;

// just do
    function_ptr myFunctionPointer = aFunction;

The reason being that if the signatures mismatch, the compiler will not complain if you’re using an explicit cast.