1

My c++ software uses another repo that collects common classes/functions. Quite often, my software only uses one of a few related classes presented in one header file. Say the external repo has a file "data_types.h", with three classes.

class X { // definition }

class Y { // definition }

class Z { // definition }

My project uses class Y from the header. When releasing to customers, I need to include my lib file together with the definition of this class Y. How can I hide the classes X and Z (since they are not used in my project) from the customers?

Is there some kind wrapper approach to recommend?

Luke
  • 720
  • 1
  • 9
  • 22
  • What do you mean by hide? Make every member of `X` and `Z` private? – Jason Sep 04 '22 at 14:19
  • 1
    I guess you are looking for PIMPL idiom? – Yksisarvinen Sep 04 '22 at 14:20
  • Related: [https://stackoverflow.com/questions/8972588/is-the-pimpl-idiom-really-used-in-practice](https://stackoverflow.com/questions/8972588/is-the-pimpl-idiom-really-used-in-practice) – drescherjm Sep 04 '22 at 14:23
  • This cannot be done in C++, exactly as described. C++ does not work this way. – Sam Varshavchik Sep 04 '22 at 14:23
  • @drescherjm It's a different topic. I am not looking for hiding private members of a class. I need to hide unused classes. – Luke Sep 04 '22 at 14:41
  • Then most likely the comment from @SamVarshavchik applies if you can't modify the headers to remove classes. – drescherjm Sep 04 '22 at 14:47
  • Seems like this calls for two headers. One defines the class `Y`, and that's available to users. The other defines the classes `X` and `Z` and has a `#include` for the header that defines `Y`. – Pete Becker Sep 04 '22 at 15:08
  • If you're just using references and pointers to the type, a forward declaration in the headers could do the trick for you... – fabian Sep 04 '22 at 15:13

1 Answers1

5

You need to split such headers into at least two, so that you can ship the necessary one without the unnecessary parts.

If you cannot modify the headers, an alternative is to simply copy-paste the necessary part(s) into a new header, and keep using the original header internally when you build if you need other parts internally. So long as you define the classes exactly the same in the reduced version of the header, this is valid. If you define them differently, you will violate the ODR.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436