假设我有以下课程:
class Airplane
{
virtual bool Fly(uint64_t destinationID)
{
//Do what an airplane does to be flown.
}
/*
* More function and data members.
*/
}
class SomeModel: public Airplane
{
virtual bool Fly(uint64_t destinationID);
{
//Do something that SomeModel specifically should do before it gets flying.
//Do exactly what Airplane::Fly does.
}
}
我的问题是如何实现 SomeModel::Fly。一种简单的方法如下:
virtual bool SomeModel::Fly(uint64_t destinationID)
{
//Do something that SomeModel specifically should do before it gets flying.
Airplane::Fly(destinationID);
}
有更好的方法吗?还是有其他理由选择另一种方式。我知道这是一个普遍的问题,但这是我第一次必须实现这样的方法,所以我想确保我没有遗漏任何东西。
编辑
我觉得值得强调的是,Airplane 不是一个通用或抽象的类,公司中的许多 Airplane 只是飞机,并没有任何继承性,但有一个特定的模型,尽管它具有一些特定的行为。