Following is the code snippet to override exception::what() function, but what I couldn't figure out how removing const-ness of the function produces following null terminated character string
"std::exception".

What I could think of is that since exception::what() is const its overriding should also be const. But, even if it is not const, there shouldn't be any problem as myexception::what() doesn't try to modify its member data.

Any thought is appreciated.

#include <iostream>
#include <exception>
using namespace std;

class myexception: public exception 
{
    virtual const char* what() const throw()
    //if const after what() not present 'std::exception' is output instead of 'My exception happened'
    {
        return "My exception happened";
    }
} myex;

int main () 
{
    try
    {
        throw myex;
    }
    catch (exception& e)    //similar case as comparedto line 10 if & is removed
    {
        cout << e.what() << endl;
    }
    return 0;
}

The problem is not to do with you code it is to do with prototype, consider this program

#include <iostream>

 class Test
 {
 public:
    const char *text() const throw()
    {
      return "Const test";
    }

    const char *text() throw()
    {
      return "Non-Const test";
    }
 };

int main()
{ 
  Test t;
  const Test* pct = &t;

  std::cout << t.text() << " : " << pct->text() << std::endl;

  return 0;
}

This compiles, but there are 2 functions call text in the class test. The difference is that one is const and one isn't. The reason this compiles is that the cv_qualifier of the function (i.e. const) is part of the things consider when looking for function overloads, so Test::text has 2 overloads.

This is what is happening when you remove the const from the what in your exception class. Instead of providing an override of virtual std::exception::what() const, your method no longer matches and is an overloaded function instead of an override of the virtual function. Then when you call what through std::exception& it calls the base class method not your method because your method no-longer overrides it.

Is it so that e in 'exception& e' is const reference? If so, how?

No, it is not about exception& e being const. It is about the base-class function char* what() const; not being the same function as the derived class function char* what();.

Imagine it this way, consider that a function marked as "const" has a different name. As so:

class Base {
  public:
    virtual void func_const() const;
};

class Derived : public Base {
  public:
    void func();
};

void foo( Base& b ) {
  b.func_const();
};

As you can see, when you call b.func_const();, it is clearly not going to call the derived class function func(), because they are different functions (i.e., func does not override func_const). Now, as far as the compiler is concerned, a const and a non-const member function are two different function signatures (even though they have the same name). So, in your case, the what() function that you have in your derived exception class does not match the signature of the base-class function, and thus, are two different functions and one does not override the other, period.

Thanks for the clarification. Not particularly related to the doubt, but i was just curious as to where 'std::exception' is mentioned in library. I looked up in the 'exception' include file. There, just before bad_exception::what() function it was mentioned to refer to 'eh_exception.cc', but i wasn't able to find it. It would be great if you could guide me in this regard.

This article has been dead for over six months. Start a new discussion instead.