【发布时间】:2021-08-08 14:45:50
【问题描述】:
代码无法打印True,因为由于某种原因比较失败。我不知道它是什么,但如果我将 e.what() == "Something Bad happened here" 更改为 e.what() == std::string("Something Bad happened here") 就可以了
#include <iostream>
#include <string>
#include <stdexcept>
int main() {
try
{
throw std::runtime_error("Something Bad happened here");
}
catch(std::exception const& e)
{
if(e.what() == "Something Bad happened here") {
std::cout << "True" << "\n";
}
}
}
【问题讨论】:
-
what()返回const char*,你在比较两个指针
标签: c++ string pointers string-comparison