lundi 24 avril 2017

Design of a Wrapper Class in C++

I have to work with an old class with a very clumsy interface. Since I cannot change it and am reliant to it, I want to build a wrapper, offering a clean interface. Let's say I have a class ClumsyClass. Basically, I have three approaches:

1. Reference Member

    Class Wrapper {
      public:
        Wrapper (ClumsyClass& clumsyClass)
          : m_clumsyClass(clumsyClass)
        { }

        int getSmth() {
          return m_clumsyClass.getSmth();
        }

        private:
          ClumsyClass& m_clumsyClass;
}

2. Pointer Member

    Class Wrapper {
      public:
        Wrapper (ClumsyClass* clumsyClass)
          : m_clumsyClass(clumsyClass)
        { }

        int getSmth() {
          return m_clumsyClass->getSmth();
        }

        private:
          ClumsyClass* m_clumsyClass;
}

3. Inheritance

    Class Wrapper : public ClumsyClass {
    ...
}

Which approach is the "cleanest" way to implement a wrapper? I prefer the third one, but when I already have a ClumsyClass object and then create a Wrapper object (copy constructor), more memory will be needed (since an instance of the original class is necessary in my case).

Aucun commentaire:

Enregistrer un commentaire