Jump to content


[C++] Callback functions in C++


4 replies to this topic

#1 TheJjokerR

    New Member

  • Members
  • PipPip
  • 12 posts

Posted 12 March 2011 - 03:53 PM

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

#2 tobeythorn

    Valued Member

  • Members
  • PipPipPip
  • 189 posts

Posted 12 March 2011 - 04:35 PM

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/...t.html#callconv

#3 TheJjokerR

    New Member

  • Members
  • PipPip
  • 12 posts

Posted 13 March 2011 - 02:14 PM

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?

#4 tobeythorn

    Valued Member

  • Members
  • PipPipPip
  • 189 posts

Posted 13 March 2011 - 03:32 PM

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.

#5 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 14 March 2011 - 03:21 PM

You could also use an interface

tobeythorn said:

			(*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.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users