How to hide some class fields in C ++

I would like to open the source code of the project, but I have a header file that looks like this:

class Foo {
 public:
  int bar;
  int super_secret_field;
};

I want the open source header to have a field bar, but not super_secret_field. I still want the closed source version with super_secret_field.

I cannot use two separate header files because classes using Foowill have strange variations. I could do something hacked with C # includes and then cross them out in the released version, but I would prefer not to.

Is there a better way?

+4
source share
2 answers

I believe the solution to your problem is the PIMPL idiom.

:

// .h
class SecretType; // forward decl

class Foo {
 public:
  int bar;
  SecretType* secret_field;
};

.cpp , SecretType. SecretType .

P.S: int , .

+6

, , - PIMPL - - .

0

Source: https://habr.com/ru/post/1529494/


All Articles