C++ 将两个 const char* 组合在一起

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18580262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:02:36  来源:igfitidea点击:

Combining two const char* together

c++stringcharconst

提问by 111WARLOCK111

I've tried many ways to do that, I got a void which is static and is on Console class i made, Void it self works fine:

我已经尝试了很多方法来做到这一点,我得到了一个静态的 void,它在我制作的 Console 类上,Void 本身可以正常工作:

Console::WriteLine(const char* msg)

On the other side, I got another const char* non static void which calls the Console::WriteLine void from It, I've been working on C# for around a year and on C# I can easily do something like this:

另一方面,我得到了另一个 const char* non static void,它从中调用了 Console::WriteLine void,我已经在 C# 上工作了大约一年,在 C# 上我可以轻松地做这样的事情:

string a = "Start ";
string b = a + "End";

When i call this on C++, it gives me bunch of errors:

当我在 C++ 上调用它时,它给了我一堆错误:

Player::Kill(const char* Message)
{
    Console::WriteLine("> " + Message + " <");
}

I've also tried the strcat thing and put, But it tells me to use strcat_s which doesn't really work, And also I've tried to do string instead of const char*, And tried char*, But all of them give errors for the thing I'm trying to do.

我也尝试过 strcat 的东西并放置,但它告诉我使用 strcat_s 这并不真正有效,而且我尝试使用 string 而不是 const char*,并尝试使用 char*,但所有这些都给出我正在尝试做的事情的错误。

回答by kfsone

"const" means "cannot be changed(*1)". So you cannot simply "add" one const char string to another (*2). What you can do is copy them into a non-const character buffer.

“const”的意思是“不能改变(*1)”。因此,您不能简单地将一个 const 字符字符串“添加”到另一个 (*2) 中。您可以做的是将它们复制到非常量字符缓冲区中。

const char* a = ...;
const char* b = ...;

char buffer[256]; // <- danger, only storage for 256 characters.
strncpy(buffer, a, sizeof(buffer));
strncat(buffer, b, sizeof(buffer));

// now buffer has the two strings joined together.

Your attempt to use std::string failed for a similar reason. You said:

由于类似的原因,您尝试使用 std::string 失败。你说:

std::string a = "Start";
std::string b = a + " End";

This translates to

这转化为

b = (std::string)a + (const char*)" End";

Which should be ok except that it creates an extra string, what you probably wanted is

这应该没问题,除了它会创建一个额外的字符串,你可能想要的是

std::string a = "Start";
a += " End";

If you are getting compile errors doing this, please post them (Make sure you #include ).

如果您在执行此操作时遇到编译错误,请发布它们(确保您使用 #include )。

Or you could do something like:

或者你可以这样做:

std::string addTwoStrings(const std::string& a, const std::string& b)
{
    return a + b; // works because they are both strings.
}

All of the following work: (see live demo http://ideone.com/Ytohgs)

以下所有工作:(见现场演示http://ideone.com/Ytohgs

#include <iostream>
#include <string>

std::string addTwoStrings(const std::string& a, const std::string& b)
{
    return a + b; // works because they are both strings.
}

void foo(const char* a, const char* b)
{
    std::string str = a;
    std::cout << "1st str = [" << str << "]" << std::endl;
    str += " ";
    std::cout << "2nd str = [" << str << "]" << std::endl;
    str += b;
    std::cout << "3rd str = [" << str << "]" << std::endl;
    str = addTwoStrings(a, " ");
    std::cout << "4th str = [" << str << "]" << std::endl;
    str = addTwoStrings(str, b);
    std::cout << "5th str = [" << str << "]" << std::endl;
}

int main()
{
    foo("hello", "world");
}

*1 Or more accurately, "cannot be changed in-situ" - you can use it in expressions, etc, so for example, e.g.

*1 或更准确地说,“无法原位更改”-您可以在表达式等中使用它,例如,例如

const size_t len = strlen("hello");
size_t newLen = len + strlen("world");
// but this would not be legal:
len += 2; // error: len is const.

2 "const chara + const char* b" is actually trying to add two pointersnot two strings, the result would be the address of string a plus the address of string b, the sum of which would be some random memory location

2 “const chara + const char* b”实际上是尝试添加两个指针而不是两个字符串,结果将是字符串 a 的地址加上字符串 b 的地址,其总和将是某个随机内存位置

回答by Alexis Wilke

char *is a pointer (so are "> "and " <"), you cannot add pointers together.

char *是一个指针("> "和也是" <"),你不能把指针加在一起。

However you can concatenate C++ strings using the + operator:

但是,您可以使用 + 运算符连接 C++ 字符串:

Player::Kill(const std::string& Message)
{
    Console::WriteLine(("> " + Message + " <").c_str());
}

回答by JaredPar

Instead of concatenating the strings and creating an extra temporary object, why not just output the 3 strings separately?

与其连接字符串并创建一个额外的临时对象,为什么不单独输出 3 个字符串?

Player::Kill(const char* Message)
{
  Console::Write("> ");
  Console::Write(Message);
  Console::WriteLine(" <");
}

回答by Dietmar Kühl

Since you say it's C++ code, just just this:

既然你说它是 C++ 代码,就这样:

void Player::Kill(std::string const& Message)
{
    Console::WriteLine(("> " + Message + " <").c_str());
}

Ideally, your Console::WriteLine()is declared to also take a std::string const&in which case you don't need to do the .c_str()-dance.

理想情况下,你Console::WriteLine()被声明为也采取 astd::string const&在这种情况下你不需要做.c_str()-dance。