The problem happens with code like this:
#include <cstdlib>
#include <iostream>
#include <stdexcept>
using namespace std;
int main(int argc, char** argv) {
try {
throw runtime_error("Message");
} catch (exception e) {
cout << e.what();
}
return 0;
}
I expect Message to appear. But the result was std::exception. I thought the subclass virtual functions can be called from the superclass reference. How can fix that?
C++ makes an explicit distinction between reference and value copy. Use
to catch by reference instead of value.