【发布时间】:2013-12-18 14:40:06
【问题描述】:
我正在写一个类似这样的界面:
class BaseInterface
{
virtual void MethodA() = 0;
}
class DerivedInterface : public BaseInterface
{
virtual void MethodB() = 0;
}
我想创建一个类Derived,它是-a DerivedInterface(隐含地,是-a BaseInterface,尤其是Base)。也就是说,我只想在 Derived 类中定义 MethodB - 我想在其他地方部分指定接口(即 MethodA)。
class Base : public BaseInterface
{
virtual void MethodA() {...};
}
class Derived : public DerivedInterface // somehow include Base as well
{
void MethodB() {...};
}
这在 C++ 中可行吗?
【问题讨论】:
-
忘记了
virtual? -
如果它不在
Derived的继承链中,它将无法工作,我很确定菱形链也不会在这里工作。 (即类似class Derived : public Base, public DerivedInterface) -
@Jefffrey 我做到了,谢谢 :)
-
但真的需要吗?我的意思是虚拟关键字不是那么隐含!
-
@BhupeshPant:默认情况下,成员函数在 C++ 中是非虚函数,因此您必须明确说明它,至少在第一次声明时是这样。
标签: c++ inheritance abstract-class