Private members of the pimpl class?

Is there any reason for the implementation class used in the pimpl idiom for any private members? The only reason I can really think is to protect myself from myself, i.e. Private members serve to provide some kind of contract between the class and the user, in which case the class and the user are pretty closely related, so it seems unnecessary.

+5
source share
7 answers

I think people confuse Pimpl idiom with Adapter / Bridge / Strategy templates. Idioms are language specific. Patterns can be applied to many languages.

Pimpl ++: , #include . .

, *.cpp , , , , Pimpl. , , :

// foo.h
class Foo : boost::noncopyable
{
public:
   ...

private:
   struct Impl;
   boost::scoped_ptr<Impl> impl_;
};

// foo.cpp
struct Foo::Impl
{
   // Impl method and member definitions
};

// Foo method definitions

, , Pimpl. , , , ..

2 .

+10

pImpl - , , , impl / . , .

+6

pimpl - , . , , , pimpl.

, , pimpl -, , .

+4

? , , PIMPL , .

. , , . , , . , , , , .

+2

, .

"private" "protected" . , - , , ( ).

+1

( , .)

, pimpl, , . , , pimpl, , , .

0

private .

, , , :

class Interface;

class Concrete { public: .... private: Interface* m_interface; }

class ConcreteFoo: public Interface {};

: ConcreteBar. , , Pimpl Strategy, Concrete Factory, .

If you can't think of a second way to implement the heart, just encapsulate it in the class. Thus, if you later have to refactor, you just need to compose an abstract Interfaceusing the same set of methods, change several pointers (and the constructor), and everything will be fine with you;)

0
source

All Articles