virtual const char* what() const throw()
{
}
AFAIK it\'s a function that will return a constant pointer to a mutable char. The rest I am not sure.
virtual function returning a pointer to a non-modifiable buffer of chars, taking no arguments, does not modify any member variables of the class is belongs to (i.e. can be called on const instances), and won't throw any kind of exception.
virtual
this one is used to overridden in derived class from base class const char*
This is a pointer to a constant characterwhat
Returns a null-terminated character sequence that may be used to identify any exception throw()
parameter inside the throw is empty so it will call std::unexpected for all exception#include<iostream>
#include<exception>
class Myexception:public exception
{
virtual const char* what() const throw()
{
return "My exception here";
}
} myex;
int main()
{
try()
{
throw myex;
}
catch(exception &e)
{
cout<<e.what()<<endl;
}
return 0;
}
It's a virtual function that returns a const char*. The const
at the end of the method means it is not allowed to change the state of the object it is called upon. Which means it is not allowed to modify any member variables of the object. throw()
part is the exception specification that says the method doesn't throw any exception.
Concerning the const after the functions: there are really two meanings, what the compiler understands, and what the usual programming conventions make it mean. As far as the compiler is concerned, all it does is make the this pointer a pointer to const. The const can be cast away, or various indirections used, to modify the observable state of the object. In the end, this const means whatever the programmer wants it to mean.
The usual convention, today, is that it means that the observable state of the object will not change. For some reasonable definition of "observable state".
Concerning the exception specification: an empty exception specification is a no throw guarantee, and is very important when considering exception safety. The next version of the standard has deprecated exception specifications, but it does provide some other means of specifying that a function will never throw.
From left to right:
virtual
- this function may be overridden in derived classesconst char*
- this function returns a modifiable pointer to a constant (array of) charwhat()
- this function takes no parametersconst
- this function does not modify the (non-mutable
) members of the object on which it is called, and hence can be called on const
instances of its classthrow()
- this function is not expected to throw any exceptions. If it does, unexpected
will be called.