Here is main()
:
int main()
{
B b(1,"two","three");
try
{
f1(b);
}
catch(B& b_ref)
{
cout<<"Caught B&"<<endl;
b_ref.print();
}
catch(A& a_ref)
{
cout<<"Caught A&"<<endl;
a_ref.print();
}
system("pause");
return 0;
}
Here is f1()
:
void f1(A& subject)
{
throw subject;
}
Information:
B inherits from A. A::print()
is virtual, and is reimplemented in B. The catch that catches the exception is catch(A& a_ref)
, which I guess makes sense, since the exceptions' static type (subject) is A&. BUT, why isn't B:: print()
running? Is the dynamic type "lost"? Only A::print()
runs in the line a_ref.print();
.
Can somebody explain?