I was going through the differnce between catch by value/refrence in Exception handling in c++
Came across this blog https://blog.knatten.org/2010/04/02/always-catch-exceptions-by-reference/
Tried the same and i am not getting the expected output.
#include<iostream>
using namespace std;
#include <typeinfo>
class Base {};
class Derived: public Base {};
int main()
{
try
{
throw Derived();
}
catch(Base &b)
{
cout<<typeid(b).name();
}
return 0;
}
The output i am getting is: 4Base
As i am catching by refrence the typeid(b).name() must capture Derived ?
or am i doing any thing wrong?
Base
andDerived
are not polymorphic types. Add somevirtual
functions (like avirtual
destructor) and it should work better. - Some programmer dude