c++pimpl-idiom

C++ pimpl idiom: return impl pointer in getter of API class


I use the pimpl idiom for classes in the public API of my library to benefit from it's properties like ABI stability. In my non-public code it would be convenient to have access to the impl object to directly operate on it.

Is it considered bad practice to add a getter to the API class to return the impl pointer? Why?

The client of my library couldn't use the impl obj anyway as its interface is non-public. My library internal code, on the other hand, can now operate on the impl obj.

public.h:

class PublicClass{
    struct Impl;
    Impl* m_impl;
public:
    Impl* getImpl();
};

impl.h:

struct Impl{
    int foo();
};

main.cpp:

#include "public.h"
#include "impl.h"

int main(){
    PublicClass p;
    auto pimpl = p.getImpl();
    auto interestingVal = pimpl->foo();
}

Just a design question.


Solution

  • As always, this depends on an exact situation. Some examples:

    However, if a lot of different classes depend on your getImpl, then you should rethink your design.

    Basically, I would say that you should make your getImpl private, and then friend the users of getImpl, with the standard attitude to friends (see, e.g., https://isocpp.org/wiki/faq/friends#friends-and-encap).