类中的每个公有函数本质上来说都是类与用户的一种约定
可以在派生类函数的异常规格说明中指定较少的异常或指定为不抛出异常,
因为这样不需要用户修改任何代码
//: C01:Covariance.cpp {-xo}
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Should cause compile error. {-mwcc}{-msc}
#include <iostream>
using namespace std;
class Base {
public:
class BaseException {};
class DerivedException : public BaseException {};
virtual void f() throw(DerivedException) {
throw DerivedException();
}
virtual void g() throw(BaseException) {
throw BaseException();
}
};
class Derived : public Base {
public:
void f() throw(BaseException) {
throw BaseException();
}
virtual void g() throw(DerivedException) {
throw DerivedException();
}
}; ///:~
由于Derived::f()违反了Base::f()异常规格说明,所以编译器将认为
Derived::f()是错误的
编译不了,没有main函数