必须始终在派生类中实现虚拟方法吗?
我可以写这样的东西吗?抽象接口继承
<!-- language: lang-cpp -->
class BaseInterface
{
public:
virtual void fun_a() = 0;
virtual void fun_b() = 0;
virtual ~BaseInterface();
};
class Derived : public BaseInterface
{
void fun_a() { ... };
};
class FinalClass : public Derived
{
void fun_b() { ... };
}
int main()
{
FinalClass test_obj;
test_obj.fun_a(); // use Derived implementation or fail ???
test_obj.fun_b(); // use own implementation
BaseInterface* test_interface = new FinalClass();
test_interface->fun_a(); // fail or ok ???
test_interface->fun_b();
}
上面的代码是否正确?
是否存在另一种虚拟方法outflank
?
看看这个例子:http://liveworkspace.org/code/6huYU$8它似乎工作。 – 2013-03-05 12:02:07
什么是不工作是这样的:http://liveworkspace.org/code/6huYU$10 – 2013-03-05 12:03:36
试试看看。 – 2013-03-05 12:14:19