There is the example program at line 28. You can run this code, it works.
But maybe the example is not obvious, I should explain. The "onInput" function is to be called when the user press enter after typing some text in a text field. In this function, the user will be able to create his own commands, etc... This is for making a simple console, where user can see messages and send commands. This is not related to Windows or other OS, it's for a microcontroller.
Essentially I need help with 3) and 4). What is the proper way to implement this second callback, so that in case the program only have one instance of this class, the user can pass a function without the "object" parameter, like so:
1 2 3 4 5 6 7 8
|
MyConsole console;
void onInput( const char * input )
{
if (!strcmp( input, ...
}
...
console.bindInputCallback( onInput );
|
Yes I name this "callback" because some game have scripting systems with such "callbacks" (onPlayerConnect, etc...), but when I search "C++ callback" I can't find much info about what I want to do.
Thomas1965, thanks but I have seen this already and there are many things I don't like with this way. I want it to be as simple as possible for the user of my class, that is, I dont want the user to have to create that Demo class.
My current solution is to have another function pointer member:
1 2 3
|
void bindInputCallback ( void (*func)( const char * ) ) { _inputCallback2 = func; }
...
void (*_inputCallback2)( const char * );
|
And then in the main code of MyConsole class I do
1 2 3 4
|
if ( _inputCallback != nullptr )
_inputCallback( *this, _inputTextBox );
if ( _inputCallback2 != nullptr )
_inputCallback2( _inputTextBox );
|
It's simple and works, I think I will keep it that way unless you can improve it without changing the syntax for the end user ?
I don't need to store these callbacks in a list, at least not yet.