lundi 24 août 2020

How to invoke the child member function inside the parent class member function without using the object

How to call the member function of child inside the member function of parent class This needs to accomplished as a part of gtest and gmock.

Example:

class Base
{
public:
    void my_read()
   {
      cout << "Base read\n";
   }
    void my_write()
   {
      cout << "Base write\n";
       my_read();
    }
};

class Derived: public Base
{
    public:
    void my_read()
    {
        cout << "Derived read\n";
     }
};
int main()
{
   Derived d;
   d.my_write();
   return 0;
 }

The desired output is:

   Base write
   Derived read

The actual output is:

   Base write
   Base read

As this is the situation of writing the test case, I cant change the code implementaion. Considering Derived class as a Mock class from the Base, I have specific implementation for my_read(). How to use this implementation in child without passing any other instances.

Aucun commentaire:

Enregistrer un commentaire