I have the following code:
#include <exception>
class Exception : public std::exception {
private:
const char* MESSAGE = "Exception"
public:
inline virtual const char* what() const throw() {
return this->MESSAGE;
}
};
class ShoulderROMException : public Exception {
private:
typedef Exception super;
const char* MESSAGE = "ShoulderROM exception";
protected:
static const int MAX_MESSAGE_LENGTH = 200;
mutable char composedMessage[ShoulderROMException::MAX_MESSAGE_LENGTH];
public:
virtual const char* what() const throw() {
strcpy(this->composedMessage, super::what());
strcat(this->composedMessage, " -> ");
strcat(this->composedMessage, this->MESSAGE);
return this->composedMessage;
}
};
class KinectInitFailedException : public ShoulderROMException {
private:
typedef ShoulderROMException super;
const char* MESSAGE = "Kinect initialization failed."
public:
virtual const char* what() const throw() {
strcpy(this->composedMessage, super::what());
strcat(this->composedMessage, " -> ");
strcat(this->composedMessage, this->MESSAGE);
return this->composedMessage;
}
};
This produces log entries looking like this:
Exception -> ShoulderROM exception -> Kinect initialization failed.
This is exactly what I want but I would like to avoid the obvious code duplication and can't seem to find a(n elegant) way to do so.
Would be really nice, if someone could help me out here. :)
Best regards, Lilo