简体   繁体   中英

What is the meaning of this header (virtual const char* what() const throw())?

class myexception: public exception
{
  virtual const char* what() const throw()
  {
    return "My exception happened";
  }
};

Sorry, this question may sound dumb, but I have trouble parsing the header. Can someone describe in English what the header actually means? The first thing that seems odd to me is the keyword virtual . The myexception class is not a base class and inherits from the already implemented exception class, so why use virtual here? I guess const is for the return type which is a c-style string that is const, and the other const is to make sure nothing that the this object cannot be modified (can someone tell me what that object could be?). I have no idea what throw() does exactly, never seen this syntax before.

virtual

Adds nothing, as the method being overridden is already virtual. You are correct: it can be omitted.

const char* what()

A member function named what() that takes no arguments and returns a pointer to const char .

const

The member function can be called via a const pointer or reference to an instance of this class or a derived class.

throw()

Throws no exceptions.

The virtual keyword is optional (you can skip it or explicitly write down - no difference) when you override an already virtual method from a base class (like in this case). Your remarks about the two const keywords are almost correct. It's basic C++.

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