None
None

Reputation: 2433

How to give access to public members with Pimpl?

#include <memory>

class MyClassImpl;

class MyClass {
    void Foo();

    struct MyStruct {
      int a;
      int b;
    } variable_struct;
private:
    std::unique_ptr<MyClassImpl> m_pImpl;
};
class MyClassImpl
{
public:
    void DoStuff() { /*...*/ }
    struct MyStructImpl {
      int a;
      int b;
    } variable_struct_impl;
};


// MyClass (External/User interface)
MyClass::MyClass () : m_pImpl(new MyClassImpl()) { }

MyClass::~MyClass () = default;

void MyClass::Foo() {
    m_pImpl->DoStuff();
}

For methods, that's quite clear, we need to make the forward method anyway. (Foo() forwarded to DoStuff() in the example)

Upvotes: 1

Views: 419

Answers (1)

eerorika
eerorika

Reputation: 238351

How to give access to public members with Pimpl?

You don't. The point of PIMPL is to hide all members of the Private IMPLementation, and having public access to them is entirely contrary to that point.

If you want pubic access, then don't put the member in a PIMPL.

Upvotes: 5

Related Questions