我想在C++上创建一个很好的接口,每个实现都需要在其上定义添加.
我想做这样的事情:
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.
Run Code Online (Sandbox Code Playgroud)
因为c ++不接受逆变,我的功能B& operator+(const B& other)没有实现virtual A& operator+(const A& other).有没有什么棘手的(但有点干净......)方法呢?