如果我想使用继承来避免重复下面的
common_method
方法。int A::different_method()
{ return 1; }
int A::common_method()
{ return this->different_method()+1; }
int B::different_method()
{ return 2; }
int B::common_method()
{ return this->different_method()+1; }
最佳方法是什么?
一种方式是使A
和B
从一个基类C
继承,使用新的方法:
int A::different_method()
{ return 1; }
int B::different_method()
{ return 2; }
int C::different_method()
{ return 0; }
int C::common_method()
{ return this->different_method()+1; }
但是有点烦人的是我不得不定义无用的C::different_method
。在这种情况下,最佳实践是什么?