简体   繁体   English

旧包中的C / C ++静态字符串

[英]C/C++ static string in old package

I have an old C/C++ package which I am trying to compile with CygWin because it needs Motif and other X- things. 我有一个旧的C / C ++程序包,由于要使用Motif和其他X-东西,因此我试图用CygWin进行编译。 Most of it compiles OK but there are some warnings due to lines like.... 大多数都可以编译,但是由于类似...的行而有些警告。

static String fallbackResources[] = { "Joe", ..etc.. , NULL}; 静态字符串fallbackResources [] = {“ Joe”,..etc ..,NULL};

I get the compiler warning: deprecated conversion from string constant to 'String {aka char*}' 我收到编译器警告:不建议将字符串常量转换为'String {aka char *}'

I have googled and found many suggestions to avoid this warning by changing occurrences of say "char* fred[]" to "const char* fred[]" which I have done for most of the c++ files in the package and this has worked perfectly to remove the compiler warnings. 我在Google上搜索,发现了很多建议,可以通过将出现的“ char * fred []”更改为“ const char * fred []”来避免此警告,这是我对软件包中大多数c ++文件所做的,并且效果很好删除编译器警告。

However I am stuck with the "static String" lines since when I change them by inserting "const" before "String" it makes no difference and if I change the "String" to "const char*" the warning disappears but the program doesn't compile due to an error later on where it sends the array to another function.... 但是,我坚持使用“静态字符串”行,因为当我通过在“字符串”之前插入“ const”来更改它们时,没有什么区别,并且如果我将“字符串”更改为“ const char *”,警告会消失,但是程序不会由于稍后在将数组发送到另一个函数的地方出错而无法编译...。

cannot convert 'const char*' to 'char**' for argument '7' to.... 无法将参数'7'的'const char *'转换为'char **'....

Any help would be very much appreciated. 任何帮助将不胜感激。

Thanks all, googled more and found ( https://stackoverflow.com/a/14648630/3100869 ) - the answer seems to be to use const_cast's like this: 谢谢大家,谷歌搜索更多并找到( https://stackoverflow.com/a/14648630/3100869)-答案似乎是使用const_cast这样的:

static String fallbackResources[] = { const_cast( "Joe" ) , ..etc.. , NULL}; 静态字符串fallbackResources [] = { const_cast( “ Joe” ,..etc ..,NULL};

... and the warnings go away! ...警告消失了!

The problem is that you've got two levels of const on pointers: the pointer itself and what it points to. 问题是指针上有两个const级别:指针本身及其指向的内容。 char const * is a pointer to const char (read from right to left), whereas char* const is a const pointer to char. char const *是指向const char的指针(从右到左读取),而char* const是指向char的const指针。

It seems that String in your case is a typedef for char* . 在您的情况下, String似乎是char*的typedef。 Making that const like you did turns it into a char* const . 像您一样使该const变成一个char* const You need to fix the typedef. 您需要修复typedef。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM