I have the following custom NotImplementedException Class.
#include <stdexcept>
#ifndef NOT_IMPLEMENTED_EXCEPTION_H
#define NOT_IMPLEMENTED_EXCEPTION_H
class NotImplementedException : public std::logic_error {
private:
std::string m_message, m_function;
public:
NotImplementedException(const char* function);
~NotImplementedException();
virtual const char* what() const;
};
#endif
#include "NotImplementedException.h"
NotImplementedException::NotImplementedException(const char* function) :
std::logic_error("NotImplementedException"), m_message("Function not
implemented: "), m_function(function) {}
NotImplementedException::~NotImplementedException() {}
const char* NotImplementedException::what() const {
std::logic_error::what();
return (m_message + m_function).c_str();
}
I utilize this exception in the following function.
virtual double Delta() const {
throw NotImplementedException(__FUNCTION__);
}
Here is the test in the main class:
try {
std::cout << option.Delta() << std::endl;
}
catch (std::logic_error& ex) {
std::cout << ex.what() << std::endl;
}
I don't understand why it is printing the message from what() like that. If for example, I change the function signature to std::string what() then the message prints out correctly. Am I printing out a char* incorrectly?