[ACCEPTED]-what does this function declaration mean in c++-c++
Regarding the const throw()
part:
const
means that this function (which is a member function) will not change the observable state of the object it is called on. The compiler enforces this by not allowing you to call non-const
methods from this one, and by not allowing you to modify the values of members.throw()
means that you promise to the compiler that this function will never allow an exception to be emitted. This is called an exception specification, and (long story short) is useless and possibly misleading.
0
It means that what
is a virtual member function 5 returning const char*
which can be invoked on const 4 objects(the const in the end). throw()
means that 3 it sort of guarantees not to throw anything.
check 2 out exception specifications in C++, and note that they are deprecated 1 in C++0x:)
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 onconst
instances of its classthrow()
- this function is not expected to throw any exceptions. If it does,unexpected
will be called.
0
virtual function returning a pointer to 5 a non-modifiable buffer of chars, taking 4 no arguments, does not modify any member 3 variables of the class is belongs to (i.e. can 2 be called on const instances), and won't 1 throw any kind of exception.
the function what()
takes no parameters, returns 7 a pointer
to a const char
(so you can't modify the what the 6 pointer points to, but you can modify the 5 pointer itself). It's virtual
, so its behaviour 4 can be overridden in derived classes. It 3 won't throw exceptions. It doesn't modify 2 any members of the class that it belongs 1 to.
It's a virtual function that returns a const 6 char*. The const
at the end of the method means 5 it is not allowed to change the state of 4 the object it is called upon. Which means 3 it is not allowed to modify any member variables 2 of the object. throw()
part is the exception specification 1 that says the method doesn't throw any exception.
virtual
: This means that the function can be reimplemented 7 in subclasses, and calls to the method via 6 a base class pointer will end up calling 5 the reimplementation.const char *
is not a constant 4 pointer to a mutable char - it's the other 3 way round.const
means that this method can even 2 be called on const instances of this class.throw()
means 1 that this method will not yield any exceptions.
Concerning the const after the functions: there 19 are really two meanings, what the compiler 18 understands, and what the usual programming 17 conventions make it mean. As far as the 16 compiler is concerned, all it does is make 15 the this pointer a pointer to const. The 14 const can be cast away, or various indirections used, to 13 modify the observable state of the object. In 12 the end, this const means whatever the programmer 11 wants it to mean.
The usual convention, today, is 10 that it means that the observable state 9 of the object will not change. For some reasonable 8 definition of "observable state".
Concerning 7 the exception specification: an empty exception specification 6 is a no throw guarantee, and is very important when 5 considering exception safety. The next 4 version of the standard has deprecated exception 3 specifications, but it does provide some 2 other means of specifying that a function 1 will never throw.
virtual
this one is used to overridden in derived class from base classconst char*
This is a pointer to a constant characterwhat
Returns a null-terminated character sequence that may be used to identify any exceptionthrow()
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;
}
0
It actually returns a mutable pointer to a constant character 1 block.
The rest is already explained by others.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.