c++design-patternsadaptor

What is the best way to implement the adaptor pattern in c++


I have seen that it is possible to use inheritance, such as:

class A {
};

class Legacy{
};

class B : public A, private Legacy {
};

But it is weird to me to inherit public and private from two different classes. Is there an alternative way to implement the adapter pattern?


Solution

  • Generally it is better to use composition instead of inheritance for adapters (and many other cases too):

    class B : public A {
      public:
        /* implementation of abstract methods of A with calls to Legacy */
    
      private:
        Legacy m_leg;
    };