这是来自 hackerrank“继承代码”的示例,
虽然这有效并what()返回n,但如果我注释返回what并取消注释当前注释的部分what()将返回垃圾。
它们在我看来都一样,有什么区别?
/* Define the exception here */
struct BadLengthException : public exception {
public:
int num;
string stra;
BadLengthException(int n){
this->num = n;
this->stra = to_string(this->num);
};
const char * what () const throw () {
return this->stra.c_str();
//string a = to_string(this->num);
//return a.c_str();
}
};
Run Code Online (Sandbox Code Playgroud)