3

So when trying to get in touch with the PIMPL idiom one finds two common ways of doing it:

  1. Using forward declaration outside a class:

    class PimplClass;
    class VisibleClass
    {
    private:
        PimplClass* d_ptr;
    };
    
  2. Using forward declaration inside a class:

    // *.hpp
    class VisibleClass
    {
    private:
       struct PimplClass;
       PimplClass* d_ptr;       
    };
    // *.cpp file:
    struct VisibleClass::PimplClass
    {
      int x;
    };
    

Two questions here:

  • To be honest I have no idea why the second one works. I mean the expression struct PimplClass I only do know from forward declaration but not within a class. Can someone please explain this to me?
  • Which solution to use? Where are the advantages or is it jsut a matter of taste?
VP.
  • 15,509
  • 17
  • 91
  • 161
binaryguy
  • 1,167
  • 3
  • 12
  • 29
  • Option 2 is just another forward declaration. Instead of a top-level class, you’re declaring a nested struct. But the logic is the same. – Robin Krahl Aug 14 '15 at 08:44
  • Formally, C++ doesn't have "forward declaration". There is just 'declaration". `struct PimplClass;` is a declaration and it declares that `PimplClass` is the name of a class (within the scope of `VisibleClass`) – M.M Aug 14 '15 at 08:54

2 Answers2

3
  • It's a forward-declaration too, but PimplClass is scoped inside VisibleClass.

  • That second solution has the advantage of not dumping an internal type into the global namespace. Keeping PimplClass scoped inside VisibleClass makes complete sense.

In both cases, the Pimpl idiom should generally use a std::unique_ptr to tie the lifetimes of the interface and the Impl together, rather than a raw owning pointer.

Quentin
  • 62,093
  • 7
  • 131
  • 191
2

You may do a forward declaration within class scope. So the second example is absolutely correct.

The main plus of second example is that your PimplClass can't be accessed from anywhere but from VisibleClass because it is declared (forward-declared) inside it's private section.

VP.
  • 15,509
  • 17
  • 91
  • 161