Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Cannot "extend" interface implementation to use new class variable

c++ interface oop

  • Please log in to reply
No replies to this topic

#1 charlesmann


    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 05 September 2016 - 06:19 PM

Hello everyone,


I am new to C++ and OOP and but due to personnel shortages I have been assigned to work on a C++ project :)

In order to make my point I am just oversimplifying the actual code.


I have created a new static method inside a class and I need to create unit tests for it. The method I have written is passed a pointer to an object that implements an interface:


WorkingClass::ComputeAndWrite(IWritter* wrt, int val)
  // DO lots of stuff and then write using an unknown implementation of IWritter

IWritter is an interface whose definition is as follows:


class IWritter
  virtual void WriteNow(int value) = 0;

So, in order to test my new function "WorkingClass::ComputeAndWrite", I need to perform some dependency injection and write to a mock implementation of the IWritter interface I created (I wanted to "extend" the interface and use the new class variable I added)

//Mock.h - Mock implementation of the IWritter interface - I wrote this

class Mock : Iwritter
  // I want to write to lastEmitted and be able to check its value from the test file
  // via ValidateInput. This must not be static, I need 1 per Mock instance
  int lastEmitted;

  void IWritter::WriteNow(int value);
  bool ValidateInput(int expected);

//Mock.cpp - Mock implementation of the IWritter interface
void IWritter::WriteNow(int value)
  //Trying to write to Mock's public member
  // I know it's wrong but idk how to make it right

bool ValidateInput(int expected)
  if (this->lastEmitted == expected)
    return true;
  return false;

So, In the testfile I would just create an instance of the mock emitter, pass it and the call Mock::ValidateInput to check if what was written is what I expect it to be:

void ValidateComputeAndWrite()
  Mock m = Mock();

  WorkingClass::ComputeAndWrite(&m, 1);

  //When the input is 1 the output must be 117

Long story short, when I try to compile I get an error saying that "lastEmitted" is not a member in the IWritter interface. I need to be able to write to it from the interface method I am overriding (IWritter::WriteNow) in order to access such "emitted" value from the testfile and verify the value that was written is the value I am expecting.


I understand what the error means but I am not allowed to modify the IWritter interface code (also I am not allowed to make lastEmitted a static variable), so I would like to ask for your help on this. I understand that I need to do some reading on C++ and OOP, but I am getting to it ;)


Any help would be heavily appreciated.


  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download