简体   繁体   中英

Inheriting from std::runtime_error

I'm coding my own Exception class:

class Exception : public std::runtime_error{

}

And I would like overload what() . How can I do that?

Also, what do I have to keep in mind when inheriting from std::runtime_error ?

I think that as what is part of the runtime_error I believe it's virtual, so to overload it, you'd need to create a method with the exact same method signature. So if you point your browser to: http://www.cplusplus.com/reference/exception/exception/ then you'll see the definition of what()

So to overload it you'll need to do something like this in your header file:

class Exception : public runtime_error
{
    public:
        const char* what() const throw();
}

then you'd define it thus in your cpp file:

const char* Exception::what() const throw()
{
    // do stuff
}

Hope it helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM