I would like to create a nice interface on C++ on which each implementation needs to have the addition defined, on itself.
I would like to do something like this :
class A{
...
virtual A& operator+(const A& other) =0;
...
}
// this is my interface or abstract class.
class B : A{
...
B& operator+(const B& other);
...
}
// this is the kind of implementation i would like. a B element can be added by another B element only ! At least this the constraint I am aiming at.
as c++ does not accept contravariance, my function B& operator+(const B& other)
does not implement virtual A& operator+(const A& other)
. Is there any tricky (but a little bit clean...) way to do that?