开发者

what does this function declaration mean in c++

virtual const char* what() const throw()
{

}
开发者_Python百科

AFAIK it's a function that will return a constant pointer to a mutable char. The rest I am not sure. Could anybody help?


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.


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.


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:)


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.


the function what() takes no parameters, returns a pointer to a const char (so you can't modify the what the pointer points to, but you can modify the pointer itself). It's virtual, so its behaviour can be overridden in derived classes. It won't throw exceptions. It doesn't modify any members of the class that it belongs to.


  1. virtual: This means that the function can be reimplemented in subclasses, and calls to the method via a base class pointer will end up calling the reimplementation.

  2. const char * is not a constant pointer to a mutable char - it's the other way round.

  3. const means that this method can even be called on const instances of this class.

  4. throw() means that this method will not yield any exceptions.


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.


  1. virtual this one is used to overridden in derived class from base class
  2. const char* This is a pointer to a constant character
  3. what Returns a null-terminated character sequence that may be used to identify any exception
  4. 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 actually returns a mutable pointer to a constant character block.

The rest is already explained by others.


Base class for all library exceptions.

This is the base class for all exceptions thrown by the standard library, and by certain language expressions. You are free to derive your own exception classes, or use a different hierarchy, or to throw non-class data (e.g., fundamental types).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜