Abstract class, abstract method parameters and inheritance

  abstract-class, c++, inheritance

I joined a research project in which the OOQP library is used. It uses an abstract class OoqpVector (inherited by several subclasses) and a number of methods, such as:

virtual double dotProductWith(OoqpVector& v) = 0

This method is implemented by subclasses SimpleVector and DistributedVector. Because of genericity, OoqpVector::dotProductWith allows all possible combinations:

  • SimpleVector.dotProductWith(SimpleVector)
  • DistributedVector.dotProductWith(DistributedVector)
  • SimpleVector.dotProductWith(DistributedVector)
  • DistributedVector.dotProductWith(SimpleVector)

However in practice, the method is called on vectors of the same type only (combinations 1 and 2). Therefore, the authors implemented SimpleVector::dotProductWith(OoqpVector&) and DistributedVector::dotProductWith(OoqpVector&) by dynamically casting the parameter to the current type.

Although this works, I find it a bit ugly and wonder if we can implement the same behavior without casts.

An option would be to get rid of OoqpVector::dotProductWith, declare the specialized methods SimpleVector::dotProductWith(SimpleVector&) and DistributedVector::dotProductWith(DistributedVector&), and force the calling methods to impose the two vector types to be equal. However, our code uses a bunch of OoqpVectors everywhere and is relatively generic, so this might be difficult to achieve.

Do you see a way to fix that?
Best,

Charlie

Source: Windows Questions C++

LEAVE A COMMENT