You could use PIMPL, and move all private fields to a private related class, eg.:
// foo.h
class FooPrivate; /* Forward declaration */
class Foo {
public: /* Public fields, if any */
Foo (); /* c-tor */
~Foo (); /* d-tor */
/* Public methods */
int how_old_is_frisky (); /* Sample method declaration */
private: /* No private fields */
FooPrivate *priv;
}
// foo.cpp
// may be a struct, it's plain ol' data container
class FooPrivate {
public:
/* Any data private to Foo. As FooPrivate
class is declared in .cpp file, it
will be accessible only from this file */
Bar bar;
Cat frisky;
Dog goggie;
}
/* Constructor - remember to allocate private data */
Foo::Foo ()
{
priv = new FooPrivate;
}
/* Destructor, remember to deallocate private data */
Foo::~Foo ()
{
delete priv;
}
/* Sample method definition */
int Foo::how_old_is_frisky ()
{
return priv->frisky.get_age ();
}
This way:
- Your header is a lot shorter
- Your private data is really private
- As a consequence of 2, if you change implementation of your class, you do not have to recompile every source file that uses merely interface of it.
Moreover, as shown in example, put method definitions (i.e. method bodies) in source (.cpp) files. This way they will be compiled only once. If you put them in header, they will be compiled every time header is compiled.
--- EDIT ---
The point of 2 and 3 is not to hide data members from malicious viewers' eyes. There is a "feature" in C++ that makes private members part of class interface. Ideally, private members should be part of implementation so users of class interface should not be affected in changes in implementation. Unfortunately, it's not the case in C++. When you change private members (ie. add new member), you change whole class interface, so users of your interface (i.e. all files that include your header file) have to be recompiled.
However, when you use pointer-to-implementation pattern, the only private member is one pointer, so only changes done to public members trigger recompilation of client code (unless you, of course, change priv pointer). To see real life examples, take look at Qt library, which uses PIMPL pattern extensively.