Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two really similar classes in C++ with only one different method: how to implement?

I have two classes that are almost identical, besides one method. The classes have the same data part and all the member functions but one:

class A {
private:
  double data;
public:
  double calc(){
     return data*data;
  }
   double especific(){
     return 2.0*data;
  }
 }

and the second class is identical, besides the especific method.

This member function in particular needs all the member data to calculate, so passing by value or reference is not an option. Is there a way to implement this without a lot of code duplication? Either using only one class or using templates, but not inheritance (huge performance impact).

Thanks

EDIT: Thanks for all the responses. The Strategy pattern can help in my case, I will try it and see if it works. I'm avoiding virtual inheritance as the plague based on some tests that I did in a different program. This routine will be called everywhere, and performance is a very important factor.

like image 893
Ivan Avatar asked Sep 20 '25 07:09

Ivan


2 Answers

This sounds like a job for the Strategy pattern. It can be implemented in this case as a template parameter. Often it would be implemented as a constructor parameter or a setter method on the class, but that would require inheritance to work properly.

In this case, something like:

template <class SpecificStrategy>
class A {
 private:
    double data;
 public:
    double calc(){
       return data*data;
    }
    double especific() {
       return SpecificStrategy::especific(data);
    }
};

class DoubleStrategy {
   static double especific(double data) {
      return 2 * data;
   }
};
class TripleStrategy {
   static double especific(double data) {
      return 3 * data;
   }
};

Then you can refer to:

A<DoubleStrategy> x;
A<TripleStrategy> y;

x and y will be of completely unrelated types, but it sounds like that's not what you want in this case.

Now, in my opinion using a virtual function and inheritance is the way to go. As someone else pointed out, the performance penalty isn't that large. However there are circumstances in which I could see that it would be a bad idea.

For example, if this class is intended to represent a vector in a graphics package and you're going to be doing the same transform to millions of them, then I could see how you would not want a virtual function call to be a part of the code that did the transform. In fact, you would want to avoid pointer dereferences of any kind of you could at all help it.

like image 140
Omnifarious Avatar answered Sep 21 '25 22:09

Omnifarious


If you don't make any members virtual and define your classes intelligently there should be no performance impact whatsoever from inheritence.

All inheritence is saying is "make this class like that one, but with this extra stuff". It is no different at runtime than if you'd typed the same stuff twice.

I suppose you could make a performance impact by doing a bunch of unnesscary stuff in the constructor for the parent class that the child classes don't need. But you won't be that stupid. I have faith in you.

like image 24
T.E.D. Avatar answered Sep 22 '25 00:09

T.E.D.