what does this function declaration mean in c++

前端 未结 11 1740
-上瘾入骨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:20
    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.

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

    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 讨论(0)
  • 2020-12-24 13:21

    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 讨论(0)
  • 2020-12-24 13:23

    It actually returns a mutable pointer to a constant character block.

    The rest is already explained by others.

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

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

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

    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.

    0 讨论(0)
提交回复
热议问题