Call-backs in C++

How to use call-back routines in C++

In a common client-server model the server invokes a call-back routine to pass the result of an asynchronous request to the client. The call-back routine has to be supplied by the client. In C++ however it is not allowed to provide a method to be the call-back routine. This implies that the call-back routine is de-coupled from the requesting object. The call-back routine is defined outside the scope of the object. A solution for this problem is to provide the request of the client not only with a pointer to the call-back routine, but also with a pointer of the calling object (also known as this). The server however has to support this additional argument and has to keep it. On completion the server calls the call-back routine with the result parameter, together with the this pointer. The call-back routine is now able to call any public method of the calling object. The connection is reestablished. If the call-back routine is defined as friend, private methods are also accessible. A rare example in which the friend concept is acceptable. In general the friend concept in C++ is not recommended!