mardi 17 septembre 2019

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**strong text**

Aucun commentaire:

Enregistrer un commentaire