How to write a C++ header file that can be #included in a C source file when inheritance is used?
I inherited a piece of software which uses both C and C++.
A C++ header file "netlib_implementation.h" has:
(s_libReceiveSocket and s_libSendSocket are "subclasses" of s_libSocket.)
A C++ program file "netlib.cc" has:Code:typedef struct s_libSocket t_libSocket; struct s_libSocket { struct sockaddr sockAddr; int IP; int socketDescriptor; } typedef struct s_libReceiveSocket t_libReceiveSocket; struct s_libReceiveSocket : s_libSocket { pthread_mutex_t threadMutex; .... ; } typedef struct s_libSendSocket t_libSendSocket; struct s_libReceiveSocket : s_libSocket { pthread_cond_t cond; .... ; }
A C program file "comm.c" calls the C++ functions:Code:#include "netlib_implementation.h" typedef t_libSocket * netHandle; typedef t_libReceiveSocket * netRHandle; typedef t_libSendSocket * netSHandle; extern "C" netSHandle netOpenSendSocket( ... ) { t_libSendSocket *s = malloc(...); ... return s; } extern "C" netRHandle netOpenSReceiveSocket( ... ) { t_libReceiveSocket *s = malloc(...); ... return s; }
Now the question is: How to write "netlib.h"?Code:#include "netlib.h" netHandle comm_handle; if (...) comm_handle = netOpenReceiveSocket( ... ); else comm_handle = netOpenSendSocket( ... );
(Note that I cannot #include "netlib_implementation.h" in comm.c because "netlib_implementation.h" has some inheritance stuff in it and a C compiler does not recognize such.)
Currently "netlib.h" is written as:
Note that the return type of the two function prototypes is netHandle, not netRHandle and netShandle as in the function definition. Also, there would be two different declarations of netHandle, one as void * in "netlib.h", another as t_libSocket * in "netlib.cc". That would be quite confusing.Code:typedef void * netHandle; netHandle netOpenReceiveSocket( ... ); netHandle netOpenSendSocket( ... );
Is there a better way to handle this?



LinkBack URL
About LinkBacks



, though I still do not know exactly why it works - specifically why C (specifically gcc 3.4.4 on Solaris 9) would accept a prototype