Following is the code snippet to override exception::what() function, but what I couldn't figure out how removing const-ness of the function produces following null terminated character string
"std::exception".
What I could think of is that since exception::what() is const its overriding should also be const. But, even if it is not const, there shouldn't be any problem as myexception::what() doesn't try to modify its member data.
Any thought is appreciated.
#include <iostream>
#include <exception>
using namespace std;
class myexception: public exception
{
virtual const char* what() const throw()
//if const after what() not present 'std::exception' is output instead of 'My exception happened'
{
return "My exception happened";
}
} myex;
int main ()
{
try
{
throw myex;
}
catch (exception& e) //similar case as comparedto line 10 if & is removed
{
cout << e.what() << endl;
}
return 0;
}