// Simulate Java Interface in C++
/*
interface IOne {
void MethodOne(int i);
.... more functions
}
interface ITwo {
double MethodTwo();
... more functions
}
class ABC implements IOne, ITwo {
// implement MethodOne and MethodTwo
}
*/
在C++中,一般情况下,我们应该避免使用多重继承,虽然多重继承在某些情况下确实有其优势。
class ABC {
public:
virtual void MethodOne(int /*i*/) = 0 {}
virtual double MethodTwo() = 0 {}
virtual ~ABC() = 0 {}
protected:
ABC() {} // ONLY ABC or subclass can access it
};
问题1:> 基于ABC
的设计,我是否需要改进其他方面以使其成为一个优秀的ABC?
问题2:> 一个好的ABC
是否应该不包含成员变量,而是将变量保存在子类中?
问题3:> 如我在评论中所述,如果ABC
必须包含太多纯函数,有更好的方法吗?
virtual
关键字;仅在最初声明为virtual
后,该关键字是可选的。 - ssube