lundi 10 octobre 2016

Bridge design pattern with inheritance where abstract base class has member data

I use bridge design pattern regularly. But I am confused as to how to use it effectively in an inherited object. Using struct apple::impl :public fruit results in double object fruits. I want to use the abstract base class data member in the implementation

class fruit
{
public:
    virtual void print() = 0;
private:
    int count_;
};

class apple: public fruit
{
    virtual void print();
private:
    int apple_count_;
    struct impl;
    std::unique_ptr<impl> impl_;
};

struct apple::impl
{
    virtual void print() { cout << "apple"; }
};

void apple::print(){ impl_->print(); }

Aucun commentaire:

Enregistrer un commentaire