Hiren
Hiren

Reputation: 77

Call a method of a different class in base class method

#include <iostream>

using namespace std;

//can't modify it
class Orig{
    public:
    void Method(){
        cout << "I am original method";
    }
};

class Mock{

  public:
  void Method(){
      cout << "I am  mock method";
  }
};


//can't modify it
class UseMethod{
    Orig object;

    public:
    void UseOrigMethod(){
        object.Method();
    }
};

class UseMethodMock : public UseMethod{
   //some code
};

int main()
{
    UseMethodMock o;
    o.UseOrigMethod();
}

I want to call Method() of Mock class using above code when I call o.UseOrigMethod(); from main. I have tried declaring object of Mock class in UseMethodMock but still it uses Orig's object.

Is there any way we can fool compiler and call Mock's Method when it calls object.Method() in UseMethod class?

I can change code in Mock and UseMethodMock classes but not in others.

Upvotes: 0

Views: 66

Answers (3)

Kishan Donga
Kishan Donga

Reputation: 3193

you just need to change the UseMethod class Orig object to Moke object i will give expected output it means called the method of Moke class.

code:

#include <iostream>

using namespace std;

//can't modify it
class Orig{
    public:
    void Method(){
        cout << "I am original method";
    }
};

class Mock{

  public:
  void Method(){
      cout << "I am  mock method";
  }
};


//can't modify it
class UseMethod{
    //need to change here Orig to Moke
    Mock object;

    public:
    void UseOrigMethod(){
        object.Method();
    }
};

class UseMethodMock : public UseMethod{
   //some code
};

int main()
{
    UseMethodMock o;
    o.UseOrigMethod();
}

output

I am  mock method
--------------------------------
Process exited after 0.006435 seconds with return value 0
Press any key to continue . . .

Upvotes: 0

Because you call orig's method. You should override useOrigMethod() in UseMethodMock class.

Upvotes: 0

Thomas
Thomas

Reputation: 181745

There is no shame in modifying code to make it more testable, so maybe you can reconsider. In this case:

  1. Prepare Orig for inheritance by making its methods virtual.
  2. Inject an Orig instance into UseMethod via its constructor (beware slicing – pass by pointer or reference).
  3. Have Mock inherit from Orig and override the virtual methods.

If the performance of virtual calls is a concern, do it all at compile time:

  1. Make UseMethod a template, that takes the type of Orig as a template parameter.
  2. In production code, use UseMethod<Orig>.
  3. In test code, use UseMethod<Mock>.

If you really, absolutely, positively can definitely, certainly not modify Orig and UseMethod, you can abuse the preprocessor:

#include "Orig.h"
#define Orig Mock
#include "UseMethod.h"
#undef Orig

Of course, this comes with a whole boatload of caveats (most notably, it assumes that UseMethod.cpp does not refer explicitly to the Orig type), and in general I would strongly advise against it.

Upvotes: 2

Related Questions