C++ Virtual Method
If I create a struct:
struct joinpoint_exception: exception
{
virtual const char* what () const throw ();开发者_StackOverflow社区
};
What does what () const throw ()
means in this context?
what
is a virtual member function returning a pointer to constant char
which is itself constant and throws nothing.
virtual const char* what () const throw ();
|-----| <- virtual member function
|---------| <- returning a pointer to constant chars
|-----| <- named what
|---| <- which is constant
|-------| <- which does not throw
(Technically the function can still throw, but if it does, it goes directly to std::unexpected
, which defaults to calling std::terminate
)
what
is name of the method
const
means that the method does not alter any internal data unless its mutable
throw ()
means that the method should not throw an exception, if it does std::unexpected
is thrown instead
It means that what()
is const
(i.e. it won't modify the logical state of the object) and that it doesn't throw any exceptions (as indicated by throw()
).
精彩评论