将uString转换为Const char * [英] Converting uString to Const char *
问题描述
伙计们,
我想问一些有关转换变量的问题.
故事发生在我拥有时:
Hola guys,
I wanna ask something about converting variable.
The story goes when I have :
uString text;
我需要他将其转换为const char *.
我已经尝试过将std :: string转换为const char的方法,但是没有用.
which I need he to be convert as const char *.
I''ve tried the same way how to convert std::string to const char, but it didn''t work.
std::string str;
char * writable = new char[str.size() + 1];
std::copy(str.begin(), str.end(), writable);
writable[str.size()] = '\0';
delete[] writable;
// NOOTTTT WORRRKKKK FOR MEE!!!!//////
有人知道怎么做吗?
谢谢
Anybody knows how to do it?
Thanks
推荐答案
我不确定,但这听起来像您有一个将const char *作为输入的函数,而您所拥有的只是一个std :: string.如果是这种情况,您现在可以忘记const部分-在那里可以向您指示,并且可以由编译器强制执行,该函数在执行过程中不会对该变量进行任何更改. -这将积极防止将数据写入引用的var.
但是,回到眼前的问题,如何将std :: string转换为更有用的东西.
普通的简单1衬里怎么样?
I can''t be sure, but it sounds like you have a function that takes a const char* as an input, and all you have is a std::string. If that''s the case, you can forget about the const part for now - that''s there to indicate to you and be made enforceable by the compiler that no changes will happen to that variable during the function. - This will actively prevent writing data to a var that''s a reference.
But, getting back to the issue at hand, how to convert a std::string into something more useful.
How about just this plain and simple 1 liner?
char *writeable = str.c_str();
您可以在此处找到文档: http://www.cplusplus.com/reference/string/string/c_str/ [ ^ ]
请特别注意,您(a)不必自己分配内存(b)您不必自己释放内存(c)您不应尝试对其进行修改-它是const char *
You can find the docs here: http://www.cplusplus.com/reference/string/string/c_str/[^]
Note specifically, you (a) don''t have to allocate the memory yourself (b) you don''t have to free it yourself (c) you shouldn''t try to modify it - it''s a const char*
这篇关于将uString转换为Const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!