what does this function declaration mean in c++

前端 未结 11 1741
-上瘾入骨i
-上瘾入骨i 2020-12-24 13:12
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.

相关标签:
11条回答
  • 2020-12-24 13:40

    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.

    0 讨论(0)
  • 2020-12-24 13:40
    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;
    }
    
    0 讨论(0)
  • 2020-12-24 13:42

    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.

    0 讨论(0)
  • 2020-12-24 13:42

    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.

    0 讨论(0)
  • 2020-12-24 13:43

    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.
    0 讨论(0)
提交回复
热议问题