PIMPL = implementation pointer. A new class is created with a pointer to a secret object. Only the hpp of this class is laid out , in which the public methods of the secret object are mainly registered. A title of a secret object, as it were forgotten to lay out. Basically this idea was for the implementation of libraries. Minus in speed. Public.hpp:
class Secret; class Public{ public: Public(); ~Public(); void PublicMethod(); private: Secret * s ; };
Public.cpp:
Public::Public():s(new Secret){} Public::~Public(){delete s;} void Public::PublicMethod(){s->PublicMethod();}
Secret.hpp:
class Secret{ public: Secret(); void PublicMethod(); private: void PrivateMethod(); };
Another option to restrict access to private fields is to create two classes, private and public. All load directly public class, and the implementation of the private is loaded automatically.
// private.hpp class Private { protected: int privatex; int PrivateF(); }; // private.cpp # include "public.hpp" int Private::PrivateF(){return privatex+((Public*)this)->publicx;} // public.hpp # include "private.hpp" class Public : public Private { public : int publicx ; int PublicF(); }; // public.cpp # include "public.hpp" int Public::PublicF(){return privatex + PrivateF();} // main.cpp # include "public.hpp" int main(){ Public x; // x.privatex ; // нет доступа // x.PrivateF(); // нет доступа x.PublicF(); // ok x.publicx = 0; // ok } //> g++ -c main.cpp public.cpp private.cpp //> g++ main.o private.o public.o
This option does NOT increase compilation speed. Only modestly hides the implementation.