Jump to content

Check out our Community Blogs


Member Since 08 May 2016
Offline Last Active Sep 05 2016 06:20 PM

Topics I've Started

Cannot "extend" interface implementation to use new class variable

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.


Hello coders!

08 May 2016 - 07:01 PM

Hello everyone,


I am new around here and I would like to say hi!


Most of my career (well I finished college about 1 year ago) I have been mainly a low-level developer (C and x86 assembly) with some experience with Java, C# and SQL.


Right now I do code in C for a living but I would like to "move higher in the development stack" so I am very eager to learn web-oriented technologies such as Java EE, Spring, Struts, JSP, JSF, Hibernate, nodeJS and such (I have been told that these technologies are a must-know for web development but I am just beginning to get acquainted to them). As I have never "really" worked in web oriented technologies I sometimes struggle to see the different layers in web-oriented development, but my dream job would be something in the likes of "backend" web development so I want to prepare for such a job.


Therefore, I think I will be throwing a lot of questions regarding these technologies, but I will also try to contribute with as much answers as possible to low-level programming questions :)


Happy coding guys and I will see you around.

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