C ++ - base class and private header

I am writing a C ++ library and have a class hierarchy as follows:

file message.h (in./mylib/src)

class Message { }; 

request.h file (in. / mylib / include / mylib)

 #include "message.h" class Request : public Message { }; 

response.h file (in. / mylib / include / mylib)

 #include "message.h" class Response : public Message { }; 

I want everything in my mylib / src folder to be hidden from the user and only want to distribute files in mylib / include. But the problem is that both requst.h and response.h #include message.h, so the user will get a "No such file" error when #including request.h and response.h. Is there any way around this problem?

+4
source share
3 answers

You can simply provide an open interface for Message and save the hidden class:

 class IMessage { Message* pImpl; }; 

Distribute this header and use the forward declaration for Message .

Another option is to use composition instead of inheritance (you will need pointers as members, not a complete object).

+6
source

If you want to use Response and Request , you need to include the header files where they are declared. This is why you should put these headers in a public folder.

+4
source

The base class must be publicly distributed, otherwise you will have to write serialization / deserialization mechanisms.

0
source

All Articles