what does this function declaration mean in c++

From left to right:

  • virtual - this function may be overridden in derived classes
  • const char* - this function returns a modifiable pointer to a constant (array of) char
  • what() - this function takes no parameters
  • const - 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 class
  • throw() - this function is not expected to throw any exceptions. If it does, unexpected will be called.

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.

It means that what is a virtual member function returning const char* which can be invoked on const objects(the const in the end). throw() means that it sort of guarantees not to throw anything.

check out exception specifications in C++, and note that they are deprecated in C++0x:)

Tags:

C++