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.