about what() in exceptions

Hi,

I saw this code in c++ tutorial of this site:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// standard exceptions
#include <iostream>
#include <exception>
using namespace std;
class myexception: public exception
{
virtual const char* what() const throw()
{
return "My exception happened";
}
} myex;
int main () {
try
{
throw myex;
}
catch (exception& e)
{
cout << e.what() << endl;
}
return 0;
}


but i can not understand what is that const ?I have underlined it.

why is it there?
Last edited on
A const member function is a function that doesn't change the data inside the class.
So this means what() won't change data inside the std::exception class.

Example of another const function:
http://cplusplus.com/reference/string/string/length/
Topic archived. No new replies allowed.