将两个 const char* 组合在一起

2022-01-12 00:00:00 string char constants c++

我尝试了很多方法来做到这一点,我得到了一个静态的 void,并且在我制作的控制台类上,Void 它自己工作正常:

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:

Console::WriteLine(const char* msg)

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

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:

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

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

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

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

我也尝试了 strcat 的东西,但它告诉我使用 strcat_s 这并没有真正起作用,而且我尝试使用字符串而不是 const char*,并尝试使用 char*,但是所有他们中的一些对我正在尝试做的事情给出了错误.

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.

推荐答案

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

"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 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.

您使用 std::string 的尝试因类似原因而失败.你说:

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

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

这转化为

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";

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

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

或者你可以这样做:

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

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

All of the following work: (see live demo 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 或者更准确地说,不能原地改变"――你可以在表达式等中使用它,例如,例如,例如

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

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 char a + const char* b" 实际上是试图添加两个 pointers 而不是两个字符串,结果将是字符串 a 的地址加上字符串 b 的地址,其总和将是某个随机内存位置

2 "const char a + const char* b" is actually trying to add two pointers not 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

相关文章