Hi,
In below design a big problem is whenever DoSomething() signature is changed to introduce new functionality caller class (return m.DoSomething()) also needs to be changed which might not be aware of this change. kindly suggest good design to get rid of this flaw.

  class MyOtherClass {
          int m;
        public:
          MyOtherClass(int x) : m(x) { }
          int DoSomething() { return m + 1; }
        }; 

        class MyClass {
          MyOtherClass m;
        public:
          MyClass() : m(42) { }
          int DoSomething() { return m.DoSomething(); }
        };

Why not derive MyClass from MyOtherClass, and make the DoSomething() function virtual? Then, you don't need to implement it in the MyClass class...

You mean you want these two classes to be, perhaps used in the same array, or code etc as if they were the same type of object?
Perhaps the adapter pattern is what you are looking for? I haven't used it that much so I defer the example code to the following link.

Click Here

I don't have any control ( Not the owner of this class)on MyOtherClass implementation so can't modify this class by declaring Dosomething virtual as both are independent classes .

(1)I don't have any control ( Not the owner of this class)on MyOtherClass implementation so can't modify this class by declaring Dosomething virtual as both are independent classes .

(2)Apart from this It is not adaptor pattern but looks like DI (dependency injection) but not completely sure.

Below solution seems to be clean and elegant solution using DI pattern.

#include <iostream>
            using namespace std;
              class MyOtherClass {
                      int m;
                    public:
                      MyOtherClass(int x) : m(x) { }
                      int DoSomething() { return m + 1; } // in future if Dosomething function supports one more additional parameter 
                    }; 

                    int setter(MyOtherClass &obj )
                    {

                    return  obj.DoSomething();//Only changes require once
                    } 
                    class MyClass {
                      MyOtherClass m;
                    public:
                      MyClass() : m(42) { }
                      //int DoSomething() { return m.DoSomething(); } //  Dosomething needs to be change here to support additional param
                      int DoSomething()
                      {
                        return setter( m);// no need to make any changes

                      }
                    };

                   class MyClass1 {
                      MyOtherClass m;
                    public:
                      MyClass1() : m(42) { }
                      //int DoSomething() { return m.DoSomething(); }  //Dosomething needs to be change here to support additional param
                      int DoSomething()
                      {
                        return setter( m); // No need to make any additional changes

                      }
                    };




            class MyClass2 {
                      MyOtherClass m;
                    public:
                      MyClass2() : m(42) { }
                      //int DoSomething() { return m.DoSomething(); } //Dosomething needs to be change here to support additional param 
                      int DoSomething()
                      {
                        return setter( m);//No need to make any additional changes

                      }
                    };



            int main() {
                // your code goes here
                MyClass obj;
                cout<<obj.DoSomething();
                return 0;
            }
This article has been dead for over six months. Start a new discussion instead.