这种特征如下:
- 项目中有一个基类和一些派生类;
- 基类某个方法的默认实现,或者某些派生类的实现是抛出一个异常;(它们可能无法提供这个方法的实现,或者有别的什么原因。)
- 有某个函数,接收这个基类为参数,并调用该方法;
- 有某个函数,调用上面一条所描述的函数,调用时传递的对象有该方法的具体实现,而不是抛出异常。
代码实例如下:
struct Base {
virtual void foo();
};
struct Derived0 : Base {
virtual void foo() { /* do something */ }
};
struct Derived1 : Base {
virtual void foo() { throw std::exception{ "Not Implemented!" }; }
};
void bar(Base &base)
{
/* do something */
base.foo();
}
void baz()
{
Derived0 d0;
bar(d0);
/* do something */
}