5

I'm implementing several classes using the pimpl idiom and am coming across some design issues.

Firstly, I've always seen pimpl done like this

class Object
{
public:
    Visible();
    ~Visible();
 .. etc ..
private:
    class ObjectImpl *_pimpl;
};

I have several classes which use this approach and my problem is that several of these classes need access to each others implementation details but the _pimpl pointer is delcared private.

Can anyone see the downside of declaring the _pimpl public. Obviously, if it's public then someone may accidentally (or deliberately) reassign it. (I'm ignoring the fact that "private" could be #defined as "public" and grant access anyway. If you do this then you deserve what you get).

I appreciate that my design may be flawed and would welcome any comments along those lines also.

I'm really loathe to use friends and am not sure they'll even help as you can't forward declare the Object::ObjectImpl without fully defining Object.

i.e.

 ...
 private:
    class ObjectImpl *_pimpl;
    friend class OtherObject::OtherObjectImpl; // this needs a fully qualified OtherObject
};

Thx Mark.

* UPDATE - More detail **

I have two classes, one called Command, the other called Results. I have methods on Command which return a vector of Results.

Both Command and Results use the pimpl idiom. I want the interface to Results to be as small as possible.

class Command
{
public:
    void getResults( std::vector< Results > & results );
    void prepareResults( std::vector< Results > & results );
private:
    class CommandImpl *_pimpl;
};

class Results
{
public:
    class ResultsImpl;

    Results( ResultsImpl * pimpl ) :
        _pimpl( impl )
    {
    }

private
    ResultsImpl *_pimpl;
};

Now in Command::getResults(). I inject the ResultsImpl into the Results. in Command::prepareResults() I need access to the ResultsImpl.

M.

ScaryAardvark
  • 2,709
  • 4
  • 27
  • 40

4 Answers4

7

I doubt there is a good reason to make the implementation public: you can always expose the implementation's functionality using public methods:

class Object
{
public:
   Object();
  ~Object();

  int GetImplementationDetail();

private:
  std::unique_ptr< ObjectImpl > _pimpl;
};

int Object::GetImplementationDetail()
{
  return pimpl->GetImplementationDetail();
}

Apart from that a class should be responsible for one thing, one thing only, and should have the bare minimum of dependencies to other classes; if you think other classes should be able to access your Object's pimpl then your design is likely flawed.

edit following the author's update: although your example is still rather vague (or at least I cannot tell the full intent of it), you seem to be misinterpreting the idiom and now try to apply it to a case where it is not usefull. As others pointed out, the 'P' stands for private. Your results class doesn't have much private implementation since all of it is public. So either try to use what I mention above and do not 'inject' anything, or just get rid of the pimpl all together and use just the Result class. If your Result's class interface should be so small that it's nothing but a pointer to another class it doesn't seem to be of much use in this case.

stijn
  • 31,563
  • 13
  • 95
  • 145
  • 1
    +1: nice touch with the `unique_ptr` :) Note that could provide a "custom deleter" (instead of defining the destructor) even though the net effect would be quite similar. – Matthieu M. Jun 24 '11 at 10:21
  • @sbi: automatic memory handling with correct destructor and move constructor/assignment for free – Matthieu M. Jun 24 '11 at 10:25
  • 1
    @sbi: `shared_ptr` is often (ill-)used for Pimpl, without due consideration about the shared semantics it implies :/ – Matthieu M. Jun 24 '11 at 12:34
  • @Matthieu: I am not convinced that implicit move semantics is that much better than implicit share semantics. What am I missing? – sbi Jun 24 '11 at 12:53
  • @sbi: when an object is "moved" there is still a unique owner (as the name of `unique_ptr` implies), thus we are still talking about "value objects", and only optimize its passing around. On the other hand, shared ownership means that several "reference objects" share the same underlying data, and thus changing to one are reflected on the others. – Matthieu M. Jun 24 '11 at 13:11
  • Hi stijn, I'm sorry this is a bit drawn out. The Results class itself does have a few more public methods but most of it will be private and since I wanted to avoid compile time dependency and also wanted to keep the implementation private I chose pimpl. ResultsImpl is where the reall party is at however, I have other classes which will need to use the ResultsImpl part of Results and I don't want to expose this in Results public interface. Hence my problem. Maybe i do have a design flaw in wanting Impls to cross-communicate.. – ScaryAardvark Jun 27 '11 at 07:55
  • this still means it's not really pimpl since the implementation is visiable to the outside. What you could do is something like this: write the getResults as getresults( std::vector& results, std::vector> ). Then in getResults you make a bunch of sherd_ptrs and inject them into the Results, plus you push them into the vector. Other classes can then access the impl they need from the vector, while the Result itself also has a reference to the same impl through it's shared_ptr. It's still a bit messy though. – stijn Jun 27 '11 at 09:34
1

Why exactly do your classes depend on details of each other? You may re-think your design. Classes should depend on abstractions.

If you really deem your design proper, nothing stops you from providing a "source-file-private" header, like:

include/
   foo.h
src/
   foo.impl.h
   foo.c
   bar.c

and then #include foo.impl.h in both foo.c and bar.c

foo.c:
    #include "foo.impl.h"
    ...

bar.c:
    #include "foo.impl.h"
    ...

But again: Generally,

Dependency Inversion Principle:

A. High Level Modules should not depend upon low level modules. Both should depend upon abstractions.

B. Abstractions should not depend upon details. Details should depend upon abstractions.

Also make very sure to check out SOLID and GRASP, especially the point about loose coupling.

Community
  • 1
  • 1
Sebastian Mach
  • 36,158
  • 4
  • 87
  • 126
  • are you suggesting that I have a ResultsImplInterface that I could then return in a public getter method on Results. Thus still keeping the actual implementation private?. – ScaryAardvark Jun 27 '11 at 07:56
  • If you hold it in a pointer or reference, just to forward it to another class later, then this would be applicable. – Sebastian Mach Jun 27 '11 at 08:00
  • Actually, having the classes depend on interfaces won't solve my immediate problem. It won't fix me having to gain access to the Results classes pimpl member which is private. Making the impl object an interface isn't changing it's protection. – ScaryAardvark Jun 27 '11 at 10:05
0

Making the the data member _pimple public won't buy you anything as long as those other classes do not see the definition of its type ObjectImpl - which is the very thing Pimple set out to prevent.

What you can do instead is to add a private interface to your Object class which will allow befriended classes to do whatever they need to do with an Object.

Of course, the common disclaimers about friend being a tool that should be used as rarely as possible all apply.

sbi
  • 204,536
  • 44
  • 236
  • 426
0

It is unnecessary to qualify the friend thusly.

If you just use friend class OtherObject, then the OtherObject class may access the necessary internals.

Personally my Pimpl are just a struct (bundle of data) and I leave the methods to operate on it in the original class.

Matthieu M.
  • 251,718
  • 39
  • 369
  • 642