Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does this function declaration mean in c++

Tags:

c++

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. Could anybody help?

like image 775
Vijay Avatar asked Mar 08 '11 09:03

Vijay


People also ask

What is a function declaration in C?

A function consist of two parts: Declaration: the function's name, return type, and parameters (if any) Definition: the body of the function (code to be executed)

What is function declaration example?

For example, if the my_function() function, discussed in the previous section, requires two integer parameters, the declaration could be expressed as follows: return_type my_function(int x, y); where int x, y indicates that the function requires two parameters, both of which are integers.

What is declaration & definition in C?

A declaration means (in C) that you are telling the compiler about type, size and in case of function declaration, type and size of its parameters of any variable, or user-defined type or function in your program. No space is reserved in memory for any variable in case of the declaration.

Does C require function declaration?

Actually, it is not required that a function be declared before use in C. If it encounters an attempt to call a function, the compiler will assume a variable argument list and that the function returns int.


2 Answers

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.
like image 90
Jon Avatar answered Oct 12 '22 22:10

Jon


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.
like image 13
Chowlett Avatar answered Oct 12 '22 21:10

Chowlett