将两个const char *组合在一起 [英] Combining two const char* together

查看:208
本文介绍了将两个const char *组合在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  Console :: WriteLine(const char * msg)

我得到了另一个const char *非静态void调用控制台:: WriteLine void从它,我一直在C#上工作了大约一年,在C#我可以轻松地做这样的:

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

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

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

我也试过了strcat的东西,使用strcat_s这不是真的工作,我也试图做字符串而不是const char *,并尝试char *,但所有的人给我的错误我想做的事情。

解决方案

const表示无法更改(* 1)。所以你不能简单地添加一个const char字符串到另一个(* 2)。你可以做的是将它们复制到非const字符缓冲区。

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

char buffer [256]; //< - danger,只存储256个字符。
strncpy(buffer,a,sizeof(buffer));
strncat(buffer,b,sizeof(buffer));

//现在缓冲区有两个字符串连接在一起。

您尝试使用std :: string失败的原因相似。您说:

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

这转换为

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

除非创建一个额外的字符串,否则应该确定

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

如果你正在编译错误,请张贴他们(确保#include)。 / p>

或者你可以这样做:

  std :: string addTwoStrings (const std :: string& a,const std :: string& b)
{
return a + b; //因为它们都是字符串。
}

所有以下工作:(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; //因为它们都是字符串。
}

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或更准确地说,不能原位更改可以在表达式等中使用它,例如

  const size_t len = strlen(hello); 
size_t newLen = len + strlen(world);
//但这不是合法的:
len + = 2; //错误:len是const。

2const char a + const char * b添加两个指针不是两个字符串,结果将是字符串a的地址加上字符串b的地址,其总和将是一些随机存储器位置


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)

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

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

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.

解决方案

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

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

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

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)

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

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

这篇关于将两个const char *组合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆