Why does the 2nd std::cout in the below code prints the "std::exception" and not "Ooops!"?
As per my understanding, since ooops class is derived from std::exception class where the what() function is virtual, when the *p (p is pointing to ooops type) is thrown from the 2nd try block, the handler(2nd catch block) should catch the ooops type and therefore ex.what should have printed Ooops! itself. Can someone please explain me where is my understanding going wrong?
The p is a std::exception* and therefore *p is a std::exception.
There are now two options:
1. Trace on runtime what is the actual type of the object pointed to by a pointer. But if you do, how will you know during compilation, how much stack space do you need for the copy?
2. Use the static type of the pointer.
Overall, this is slicing.
1 2
ooops bar;
std::exception snafu = bar;
The snafu is a plain exception. A copy of the exception-parts of the bar. The ooops-part got sliced off. We can use bar as an exception in the copy constructor of snafu, because ooops IS-A exception.