繁体   English   中英

连接const char *字符串

[英]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
  }
};

注意事项:

  1. 原始代码使用本地变量作为缓冲区。 这是一个坏主意,因为在作用域结束时,存储在cause的指针将无效

  2. 对于串联的消息,将需要动态分配。 这也意味着将需要清理该存储。 我抓住了一个现成的工具,它提供了类似字符串的操作: std::string

  3. 使用std::string串联可以使用+运算符完成。 请注意我是如何要求它为预期大小保留内存的。 这是内存的优化,不是必需的:字符串将以任何一种方式分配足够的内存。

  4. what不能抛出异常,否则调用std::unexpected会发生。 因此无法在此处分配字符串。

如果必须使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM