Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Virtual Method

Tags:

c++

If I create a struct:

struct joinpoint_exception: exception
{

   virtual const char* what () const throw ();
};

What does what () const throw () means in this context?

like image 585
Bitmap Avatar asked Jan 25 '26 07:01

Bitmap


2 Answers

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)

like image 86
Billy ONeal Avatar answered Jan 26 '26 22:01

Billy ONeal


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

like image 40
Gregor Brandt Avatar answered Jan 26 '26 21:01

Gregor Brandt