0

我有以下代码:

#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;
    }
};

这会产生如下所示的日志条目: Exception -> ShoulderROM exception -> Kinect initialization failed. 这正是我想要的,但我想避免明显的代码重复,并且似乎找不到一种(n 优雅的)方法来做到这一点。

如果有人可以在这里帮助我,那就太好了。:)

最好的问候,莉洛

4

2 回答 2

1

通过一个公共类来实现它。我会像这样重写你的代码:

class Exception : public std::exception {
    static const char* MESSAGE = "Exception"
    static const int MAX_MESSAGE_LENGTH = 200;
    mutable char composedMessage[MAX_MESSAGE_LENGTH];

public:
    virtual const char* name() const throw() {
        return MESSAGE;
    }

    virtual const char* what() const throw() {
        strcpy(this->composedMessage, name());
        strcat(this->composedMessage, " -> ");
        strcat(this->composedMessage, this->MESSAGE);
        return this->composedMessage;
    }
};

class ShoulderROMException : public Exception {
    static const char* MESSAGE = "ShoulderROM exception";
public:    
    virtual const char* name() const throw() {
        return MESSAGE;
    }
};

class KinectInitFailedException : public ShoulderROMException {
    static const char* MESSAGE = "Kinect initialization failed."
public:
    virtual const char* name() const throw() {
        return MESSAGE;
    }
};

如果您不想在Exception类中实现太多实现,请添加另一个,两者都ShoulderROMException将从中KinectInitFailedException继承。

您的代码还有其他问题:MESSAGE成员应该是static,并且您处理字符串的方式不是很C++ish。我还要补充一点,内联虚函数没有任何意义。

于 2015-12-09T11:27:02.010 回答
1

感谢你的帮助。它给了我灵感。有了一个同学的一些额外想法,我想出了这个效果很好的解决方案。:)

#include <exception>

class Exception :
    public std::exception {
private:
    static const std::string MESSAGE = "Exception";

protected:
    std::string composedMessage;

public:
    Exception() :
    composedMessage(this->MESSAGE) {
    }

    virtual const char* what() const throw() {
        return this->composedMessage.c_str();
    }
};

class ShoulderROMException :
    public Exception {
private:
    static const std::string MESSAGE = "ShoulderROM exception";

public:
    ShoulderROMException() {
        this->appendMessage(this->MESSAGE);
    }

    virtual void appendMessage(std::string message) {
        this->composedMessage += " -> ";
        this->composedMessage += message;
    }
};

class KinectInitFailedException :
    public ShoulderROMException {
private:
    static const std::string MESSAGE = "Kinect initialization failed.";

public:
    KinectInitFailedException() {
        this->appendMessage(this->MESSAGE);
    }
};

我从错误的角度看待问题:自上而下而不是自下而上。^^

无论如何,感谢您的帮助和最好的问候, Lilo

于 2015-12-09T15:46:09.540 回答