Question

I would like to know if there is a way to put only protected and public stuff on the header file .h, and all private stuff in the compile unit .cpp I need this because the library is going to be used by others, and I wouldn't like to have to copy and edit all .h files to remove private declarations and implementations. I tried but got the duplicate declaration error

another question is about private static stuff can I declare them on the header file and implement them on the .cpp unit? a private variable and a public get method I tried but couldn't implement the method on the unit, it only worked with the declaration and implementation on the header

[]s, Joe

Was it helpful?

Solution

The proper way to deal with this is to implement the pimpl idiom: Create a class or struct for all private data and put a pointer to such an object in the header file, together with a forward declaration. Now nothing of the private data and methods is visible from the header file.

OTHER TIPS

The correct answer to this is to use a Pimpl (via a pointer, as Pavel points out). There's also a crazy but potentially correct way described in Matthew Wilson's Imperfect C++, where you can forward declare an internal structure and include an opaque block of memory in your class, and then in-place construct the internal structure (whose definition is made in the implementation file) in the main class constructor in the implementation file.

I should point out that Wilson shows this in an appendix where he confesses to several such "crimes against programming" as a caution to programmers trying to be too clever. He says, and I say, that you shouldn't use this. However, if you have some overriding exacting performance requirements it's possible that it might be of some use.

Andreas has the answer but note that this will make your code somewhat more obtuse for yourself:

 // header file

 struct hidden_structure;

 class Foo {
     hidden_structure* hidden_data;
 public:
     Foo();
     ~Foo();

     void doStuff();
 };

 // your cpp file

 struct hidden_structure;
     int stuff;
     hidden_structure() : stuff(0) {}
 }

 Foo::Foo() : hidden_data(new hidden_structure) {}
 Foo::~Foo() { delete hidden_data; }

 void Foo::doStuff() { hidden_data->stuff += 34; } // hey, it does a lot of stuff

As you can see, the more data inside hidden_structure the more complex this can become.

Couldn't you declare the full class name for users, but put a class definition in the .h file that includes only the publics and privates, then in the .cpp file subclass from the public definition, and define only privates?

Like this:

.h file--

class useThis;   // users actually use this class

class publics {  //this is the interface they see
public:
    int foo;
};

.cpp file--

#include "foo.h"

class useThis: publics {

private:
    void add(int b);

};

void useThis::add( int b )
{
    foo+= b;
}

Same as John's code, but I am using a pointer instead of references:

// file.h
class TheClass_p;
class TheClass{
  public: 
    TheClass();
    ~TheClass();
  private
    TheClass_p *d;

};

// file.cpp
class TheClass_p {
  int foo;
  float: bar;
};

TheClass::TheClass(){
   d = new TheClass_p;
}

TheClass::~TheClass(){
   delete d;
}

Edit: added destructor to free a memory leak

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top