Hasan H
Hasan H

Reputation: 141

How to pass a pimpl-idiom instance from child to parent?

I have a parent class and a child class that have a Pimpl class implementation. I want to create an instance of the pimpl in the child class and pass it to the parent by the constructor of the child to the constructor of the parent.

I tried creating a parent's constructor which takes as parameter the impl class, then calling this constructor from the constructor of the child.

//ParentInterface.h

class impl; //  error message is here after I add the constructor with parameter

class ParentInterface {
public:

    ParentInterface();

    ParentInterface(impl *pimpl);

    virtual ~ParentInterface();
    ParentInterface(const ParentInterface& orig);


private:
    class impl;
    impl* pimpl;

};


//ParentInterface.cpp

class ParentInterface::impl
{
  public:
    virtual ~impl(){}
    MLB_API temp[1];

  private:
};

ParentInterface::ParentInterface():pimpl(new impl()){

}

ParentInterface::ParentInterface(impl *pimpl_){
     pimpl = pimpl_;
}

ParentInterface::~ParentInterface() {
    delete pimpl;
    pimpl = NULL;
}

Whenever I add the constructor which takes an impl class, something goes wrong in the first class impl; line. The following lines are the error message

changes meaning of ‘impl’ from ‘struct impl’

Unable to resolve forward declaration impl

Upvotes: 0

Views: 331

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136525

The compiler errors are caused by 2 different class impl: one at the global scope, another in ParentInterface. ParentInterface constructor accepts the global class impl, whereas ParentInterface::pimpl is of type class ParentInterface::impl.

To fix the compiler errors, remove the global class impl:

class ParentInterface {
protected: // Accessible to derived classes.
    class impl;
    impl* pimpl;

public:
    ParentInterface();
    ParentInterface(impl *pimpl);
    ~ParentInterface();
};

class ParentInterface::impl
{
public:
    virtual ~impl() {}
};

ParentInterface::ParentInterface()
    : pimpl(new impl)
{}

Upvotes: 1

Related Questions