C++ 在向某些继承树类引入附加接口时避免代码重复

c++ avoid code duplication when introducing additional interface to some classes of inheritance tree

本文关键字:接口 代码 继承 C++      更新时间:2023-10-16

下面是一个模型示例。假设我有具有三个坐标 x,y,z 的类 Creature。我也有继承树:

class Creature {
  int x;
  int y;
  int z;
};
class Mammal : public Creature {common code for mammals};
class Wolf : public Mammal;
class Bat : public Mammal; 
class Birds : public Creature {common code for birds};
class Penguin : public Bird;
class Crow : public Bird;

有些生物可以飞行,所以它们应该有以下成员函数(所有生物的实现都是一样的(:

void fly(int height) {z = height;}

所以我做了以下几点:

class FlyingMammal : public Mammal {
  void fly(int height) {z = height;}
  //'z' is also used in some methods of non-flying creatures, 
  //so it can't be moved to FlyingMammal class.    
};
class Wolf : public Mammal;
class Bat : public FlyingMammal;
class FlyingBird : public Bird {
  void fly(int height) {z = height;}
};
class Penguin : public Bird;
class Crow : public FlyingBird;

但是,我在FlyingMammal和FlyingBird中有代码重复。有没有标准模式来避免它?我想某种多重继承可能适合这里,但不知道如何做到这一点。

请注意,像 fly() 这样的附加接口不会引入新的数据成员,只会引入新的成员函数。但它使用Creature类的成员数据z

我认为奇怪地重复出现的模板模式 将是一种方式...这样

template <typename TThisClass>
class Flier
{
public:
    void fly(int z) { static_cast<TThisClass *>(this)->setZ(z); } 
};
class Creature {
private:
  int x;
  int y;
  int z;
public:
  void setX(int x) { this->x = x; }
  void setY(int y) { this->y = y; }
  void setZ(int z) { this->z = z; }
  // add setter & getter for x, y, z
};
class Mammal : public Creature { /*common code for mammals*/};
class Wolf : public Mammal { };
class Bat : public Mammal, public Flier<Bat> { };
class Bird : public Creature { /*common code for birds*/ };
class Penguin : public Bird { };
class Crow : public Bird, public Flier<Crow> { };
int main(int, char **) { Crow c; c.fly(1000); };