@ mcalsyn - Thanks for your example project!
Iām trying to make more changes.
One thing I am trying to do now is to pass a parameter in the callback. (The example callback you provided is to a function that takes no parameters.)
What is the syntax to adapt the example to pass a value back in the callback function?
For instance if HandleCallback were to take a char as a parameter:
void HandleCallback(char x)
{
printf("%02X",x);
}
How would you change the syntax in worker::Start?
void Worker::Start(int interval, std::function<void(void)> func)
and the std::Bind call in the client?
_worker->Start(2000, std::bind(&CSomeClient::HandleCallback, this));
so that when I call the callback I can pass a value:
func(somecharvalue);
I tried various things. but nothing seems to compile.
I suspect you got most of it right, but there is a tricky bit to adding args to the bind call - you have to provide an argument value or a placeholder. You should provide an lvalue if that value will be constant across all calls or an std::placeholder if that argument will be bound at each call. Here are the changed lines. presuming that the returned value will change with each callback. I just used āXā :
In worker.h:
becomes
```cpp]void Start(int interval, std::function<void(char)> func);[/code
in worker.cpp the call to func:
```cpp]func();[/code
becomes
```cpp]func('X');[/code
The prototype for the handler in the main app (LinuxApp.cpp in my example):
```cpp]void HandleCallback()[/code
becomes
```cpp]void HandleCallback(char ch)[/code
and the tricky bit, again in the main app (LinuxApp.cpp in my example):
```cpp]worker->Start(2000, std::bind(&CSomeClient::HandleCallback, this));[/code
becomes
```cpp]worker->Start(2000, std::bind(&CSomeClient::HandleCallback, this, std::placeholders::_1));[/code
1 Like
Ah, thanks!
I will try this when I get home
I had found std::placeholders in my searching last night, but none of my attempts to use them worked.
(fingers crossed)