Correct files for declaration and definition for callback functions

C

Chris Bore

If I have a self-contained server component, that specifies callback
functions, then would it be good practice for:

1) the server component itself to provide a header file with the
callback function declarations
2) the client to implement the function definitions

In this way the server owns the interface (usage, through the
declaration) but the client is responsible for implementation (through
the definition). There is some checking, by (server's) declaration
against (client's) implementation, that the implementation at least
matches the function's declaration.

I have not seen this separation and defined ownership in code that I
have studied, but it seems to me that it should be so. I expected to
find this documented in texts but have not so far found this. It seems
obvious but is not apparently done. I'd value any comments or examples
of how it is done in good practice.

Thanks,

Chris
 
C

CBFalconer

Chris said:
If I have a self-contained server component, that specifies callback
functions, then would it be good practice for:
.... snip ...

I have not seen this separation and defined ownership in code that I
have studied, but it seems to me that it should be so. I expected to
find this documented in texts but have not so far found this. It seems
obvious but is not apparently done. I'd value any comments or examples
of how it is done in good practice.

Take a look at the way hashlib defines and uses the necessary
auxiliary functions. It is all spelled out in hashlib.h. See:

<http://cbfalconer.home.att.net/download/>
 
P

Pierre Asselin

Chris Bore said:
If I have a self-contained server component, that specifies callback
functions, then would it be good practice for:
1) the server component itself to provide a header file with the
callback function declarations
2) the client to implement the function definitions

No. Give a typedef for a pointer to callback function. For
an example, look at how signal() is declared:

typedef void (*sighandler_t)(int);
extern sighandler_t signal(int signum, sighandler_t handler);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top