当我们使用继承的类时,c ++捕获异常

时间:2013-05-15 09:43:28

标签: c++ exception

我有这样的代码:

Class A{};

Class B : A {};

void foo()
{
  throw new B;
}

这里我们只能使用catch(B * e)捕获异常。但是为什么我们不能用catch(A * e)来捕获它。 这是因为继承中的私有访问类型吗?

1 个答案:

答案 0 :(得分:2)

是的,这正是原因。

由于B私下继承A,除了B之外,没有人知道继承。 因此,B对象不被视为A对象,无法转换为A的实例。