![](/img/trans.png)
[英]Is it possible to concatenate two strings of type `const char *` at compile time?
[英]concatenate const char * strings
我对char *
和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;
}
};
您可以存储std::string
并仍然从what函数返回const char *
。
struct MyException : public std::exception
{
private:
std::string message;
public:
MyException(const char *s, int n) {
char buffer[1024];
strerror_r(n, buffer, 1024);
message.reserve(strlen(s) + 2 + strlen(buffer));
message = s;
message += ": ";
message += buffer;
}
MyException(const char *s, const char *c) {
message.reserve(strlen(s) + 2 + strlen(c));
message = s;
message += ": ";
message += c;
}
const char *what() const throw()
{
return message.c_str();
}
};
只需使用string.h中的 strcat()
和strcpy()
函数即可。
http://www.cplusplus.com/reference/clibrary/cstring/strcat/ http://www.cplusplus.com/reference/clibrary/cstring/strcpy/
另外,由于不必修改原始字符串,因此const char*
和char*
之间的区别无关紧要。
同样不要忘记malloc()
(保留空间)目标字符串的所需大小。
这就是我要实现的方式:
struct MyException : public std::exception
{
public:
const char *source;
int number;
const char *cause;
private:
char buffer[1024]; // #1
std::string message; // #2
std::string build_message() {
if (number != 0) {
cause = strerror_r(number, buffer, 1024); // use the member buffer
}
std::string s; // #3
s.reserve(strlen(source) + 2 + strlen(cause));
return s + source + ": " + cause;
}
public:
MyException(const char *s, int n)
: source(s), number(n), cause(), message(build_message()) {}
MyException(const char *s, const char *c)
: source(s), number(0), cause(c), message(build_message()) {}
const char *what() const throw()
{
return message.c_str(); // #4
}
};
注意事项:
如果必须使用char*
指针,则需要使用strcat
。 strcat
有两个参数一个char*
和const char*
和附加字符串被指出const char*
到char*
。 这意味着您首先需要复制第一个字符串。
您将需要执行以下操作:
char* Concatenate(const char* first, const char* second)
{
char* mixed = new char[strlen(first) + strlen(second) + 2 /* for the ': ' */ + 1 /* for the NULL */];
strcpy(mixed, first);
strcat(mixed, ": ");
strcat(mixed, second);
return mixed;
}
那不是很丑吗? 并且,请记住,因为您已经动态分配了该函数返回的char *,所以调用者必须记住将delete[]
。 这很丑陋,并且需要确保调用者以正确的方式清理,这就是为什么最好使用字符串实现(例如std::string
。
分配大小为strlen(source) + strlen(cause) + 3
的缓冲区,并使用sprintf
创建消息。 其实你可以让这个代码移到构造what
变得简单吸气。
如果确实必须使用c字符串,则应查看strcat()
将它们串联在一起。 但是,由于要创建自定义异常,因此可以考虑使用std::string
代替,因为在C ++中使用它更友好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.