将两个常量char*组合在一起
Combining two const char* together
我尝试了很多方法来做到这一点,我得到了一个void,它是静态的,在我创建的控制台类上,void本身工作良好:
Console::WriteLine(const char* msg)
另一方面,我得到了另一个const char*非静态void,它从It调用Console::WriteLine void,我已经在C#上工作了大约一年,在C#上我可以很容易地做这样的事情:
string a = "Start ";
string b = a + "End";
当我在C++上调用它时,它会给我一堆错误:
Player::Kill(const char* Message)
{
Console::WriteLine("> " + Message + " <");
}
我也尝试过strcat的东西和put,但它告诉我使用strcat_s,这并不真的有效,我也尝试了字符串而不是const-char*,并尝试了char*,但所有这些都给出了我尝试做的事情的错误。
"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.
由于类似的原因,您尝试使用std::string失败。你说:
std::string a = "Start";
std::string b = a + " End";
这转化为
b = (std::string)a + (const char*)" End";
这应该是可以的,除了它创建了一个额外的字符串,你可能想要的是
std::string a = "Start";
a += " End";
如果您在执行此操作时遇到编译错误,请将其发布(请确保#包含)。
或者你可以做一些类似的事情:
std::string addTwoStrings(const std::string& a, const std::string& b)
{
return a + b; // works because they are both strings.
}
以下所有工作:(见现场演示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或者更准确地说,"不能原位更改"-你可以在表达式等中使用它,例如
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"实际上试图添加两个指针而不是两个字符串,结果将是字符串a的地址加上字符串b的地址,其总和将是某个随机内存位置
char *
是指针("> "
和" <"
也是),不能将指针添加在一起。
但是,您可以使用+运算符连接C++字符串:
Player::Kill(const std::string& Message)
{
Console::WriteLine(("> " + Message + " <").c_str());
}
与其连接字符串并创建一个额外的临时对象,为什么不单独输出这3个字符串呢?
Player::Kill(const char* Message)
{
Console::Write("> ");
Console::Write(Message);
Console::WriteLine(" <");
}
既然你说它是C++代码,就这样:
void Player::Kill(std::string const& Message)
{
Console::WriteLine(("> " + Message + " <").c_str());
}
理想情况下,您的Console::WriteLine()
被声明为也接受std::string const&
,在这种情况下,不需要进行.c_str()
舞蹈。
#include <iostream>
using namespace std;
string string1 = "John";
string string2 = "Smith";
float string1Len = string1.length();
float combinedLen = string1.length() + string2.length();
char combine[string2.length() + string1.length()];
for(int i = 0; i < string1Len; ++i){
combine[i] = string1[i];
}
for(int i = string1Len; i < combinedLen; ++i){
combine[i] = string2[i - string1Len];
}
const char * combined = combine;
好吧,可以用const char*进行更改,即使它是const,我们也不能更改它的值,但可以更改它的地址,请查看下面的类以获取参考
class String
{
private:
const char* m_Name;
int m_Size;
public:
String() :m_Name(" "), m_Size(0) { m_Size = 0; }
String(const char* name , int size) :m_Name(name),m_Size(size) {}
String(const char* name) :m_Name(name) {}
void Display()
{
LOG(m_Name);
}
int GetSize()
{
while (m_Name[m_Size] != ' ')
{
m_Size++;
}
return m_Size;
}
void Append(const char* nameToAppend)
{
//Create an empty char array pointer "*tempPtr" of size = existing const
//char name pointer "m_Name" + const char pointer appending name
//"*nameExtention"
//this array can store both the names
int totalSize = 0;
int nameToAppendSize = 0, existingNameSize = 0;
while (nameToAppend[nameToAppendSize] != ' ')
{nameToAppendSize++;}
existingNameSize = this->GetSize();
totalSize = nameToAppendSize + existingNameSize;
char* tempPtr = new char[totalSize+1];
//Add existing const char name pointer "*m_Name" + const char pointer
//appending name "*nameExtention" to tempPtr, using a loop
int currentSize = 0;
for (int i = 0; i < existingNameSize; i++)
{
tempPtr[currentSize] = m_Name[i];
currentSize++;
}
for (int i = 0; i <= nameToAppendSize; i++)
{
if (i == nameToAppendSize)
{
tempPtr[currentSize] = ' '; // this line tells compiler to stop
// writting inside tempPtr
}
else
{
tempPtr[currentSize] = nameToAppend[i];
currentSize++;
}
}
//--------------------------------
//Now change the address of your const char* with tempPtr
//This will change the contents of the const char*
//--------------------------------
m_Name = (char*)tempPtr;
}
};