views:

251

answers:

3

Can someone please give me an example of how public and private headers work? I have done some reading on the net but I can't seem to find much useful information with sample codes. I was advised that I should use private headers to separate the public and private parts of my code for creating a static library. After some reading I have a general idea of how it works, but would really appreciate a good example to get me started. Specifically, what I don't quite understand is how to put the interface functions in my public header, and the private variables/functions in my private header? Thanks!

EDIT:

Maybe I'm not wording my question right, but what I meant is, for example, I have myMath.h and myMath.cpp, and myMath.h has:

class myMath{
public:
    void initialise(double _a, double _b);
    double add();
    double subtract();

private:
    double a;
    double b;
};

And myMath.cpp has the implementations of the functions. How can I make it so that myMath.h only has the three public functions, and the private variables are defined in another file (e.g. myMath_i.h), and these three files are in such a way that after I create a static library, only myMath.h is needed by users. This also means myMath.h cannot #include myMath_i.h. So something like:

myMath.h:

class myMath{
public:
    void initialise(double _a, double _b);
    double add();
    double subtract();
}

and myMath_i.h:

class myMath{
private:
    double a;
    double b;
}

Of course that's not possible because then I'll be redefining the class myMath...

+1  A: 

You can declare all the interfaces and constants you want to expose to the library user in a separate header file (or a set of files) and put it into "inc" subdirectory - those headers will be "public". You will also use other header files to declare classes and stuff you don't want to expose since theay are implementation details - you will put those files into "src" subdirectory - those will be "private".

This way the user will be given a hint that he has to include only the public headers - those that are in "inc" and only those headers contain the stuff he really needs, while all other headers are "private" and out of his interest area unless he wants to read into the implementation.

When you publish your library as binary - either static or dynamic library - you only copy the "inc" contents and the compilation result - those are enough for the user and this way he doesn't see your implementation sources.

sharptooth
+1  A: 

Public headers are those needed by the users of the library. Private headers are those needed to compile the library, but which are not needed by library users. Performing the split can be quite tricky, and a lot of libraries simply don't bother.

anon
A: 

You have two header files MyClass.h and MyClass_p.h and one source file: MyClass.cpp.

Lets take a look at what's inside them:

MyClass_p.h:

// Header Guard Here
class MyClassPrivate
{
public:
    int a;
    bool b;
    //more data members;
}

MyClass.h:

// Header Guard Here
class MyClassPrivate;
class MyClass
{
public:
    MyClass();
    ~MyClass();
    void method1();
    int method2();
private:
    MyClassPrivate* mData;
}

MyClass.cpp:

#include "MyClass.h"
#include "MyClass_p.h"

MyClass::MyClass()
{
    mData = new MyClassPrivate();
}

MyClass::~MyClass()
{
    delete mData;
}

void MyClass::method1()
{
    //do stuff
}

int MyClass::method2()
{
    return stuff;
}

Keep your data in MyClassPrivate and distribute MyClass.h.

erelender
This goes further than just providing public and private headers and shows the pImpl idiom too -- public/private is necessary for that, but pImpl isn't required.
Andrew Aylett