【发布时间】:2012-07-26 19:57:08
【问题描述】:
我对@987654321@ 和const char * 感到困惑。在我的示例中,我不确定如何将它们组合在一起。我有几个 const char * 字符串我想连接到最终的 const char * 字符串。
struct MyException : public std::exception
{
const char *source;
int number;
const char *cause;
MyException(const char *s, int n)
: source(s), number(n) {}
MyException(const char *s, const char *c)
: source(s), number(0), cause(c) {}
const char *what() const throw()
{
if (number != 0) {
char buffer[1024];
// why does this not work?
cause = strerror_r(number, buffer, 1024);
}
// how to concatenate the strings?
return source + ": " + cause;
}
};
【问题讨论】:
-
为什么要使用 cstrings?你受限于他们吗?我建议你使用 std::string。
-
这是一个练习吗?如果不是,您可以使用 std::strings 让生活更轻松??
-
同上,同上。使用标准::字符串。更好地解决 cstrings 的大多数问题。
-
@Zeta:在投掷过程中,它不应该...所有从 std::exception 派生的
类都使用 std::string!
标签: c++ string char constants concatenation