如何在不重复代码的情况下在子类中获取相同的方法代码?
How can I get the same method code in a subclass without code duplication?
我有以下代码:
#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.
这正是我想要的,但我想避免明显的代码重复,而且似乎找不到(优雅的)方法来做到这一点。
如果有人能帮助我,那就太好了。 :)
此致,
星际宝贝
通过一个通用的class实现它。我会像这样重写你的代码:
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
class 中有太多实现,请添加另一个 ShoulderROMException
和 KinectInitFailedException
都将继承的实现。
您的代码还有其他问题:MESSAGE
成员应该是 static
,并且您处理字符串的方式不是很 C++ish.我还要补充一点,内联虚函数没有任何意义。
感谢您的帮助。它给了我灵感。借助一位同学的一些额外想法,我想出了这个非常有效的解决方案。 :)
#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);
}
};
我从错误的角度看问题:自上而下而不是自下而上。 ^^
感谢您的帮助和最诚挚的问候,
星际宝贝
我有以下代码:
#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.
这正是我想要的,但我想避免明显的代码重复,而且似乎找不到(优雅的)方法来做到这一点。
如果有人能帮助我,那就太好了。 :)
此致, 星际宝贝
通过一个通用的class实现它。我会像这样重写你的代码:
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
class 中有太多实现,请添加另一个 ShoulderROMException
和 KinectInitFailedException
都将继承的实现。
您的代码还有其他问题:MESSAGE
成员应该是 static
,并且您处理字符串的方式不是很 C++ish.我还要补充一点,内联虚函数没有任何意义。
感谢您的帮助。它给了我灵感。借助一位同学的一些额外想法,我想出了这个非常有效的解决方案。 :)
#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);
}
};
我从错误的角度看问题:自上而下而不是自下而上。 ^^
感谢您的帮助和最诚挚的问候, 星际宝贝