C++ 警告:不推荐将字符串常量转换为 'char*''
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7468286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
warning: deprecated conversion from string constant to 'char*''
提问by venkysmarty
Possible Duplicate:
How to get rid ofdeprecated conversion from string constant to ‘char*'
warnings in GCC?
可能的重复:
如何消除deprecated conversion from string constant to ‘char*'
GCC 中的警告?
I use following function from library which i cannot change:
我使用无法更改的库中的以下函数:
HRESULT DynamicTag(char * pDesc, int * const pTag );
I use it as follows. I have created the object of the class provided by the library that implements the above function.
我使用它如下。我已经创建了实现上述功能的库提供的类的对象。
int tag =0;
g_pCallback->DynamicTag("MyLogger", &tag);
I am getting following warning:
我收到以下警告:
warning: deprecated conversion from string constant to 'char*'
What is the best way of getting rid of above warning? I don't want to allocate memory dynamically.
摆脱上述警告的最佳方法是什么?我不想动态分配内存。
Info: I am using Vxworks6.8 compiler
信息:我使用的是 Vxworks6.8 编译器
采纳答案by Suma
Dealing with unknown library
处理未知库
When passing literals and not other const string, and you are not sure if the library is modifiying the string, is easy to create a stack allocated temporary copy of the literal in C++ (inspired by How to get rid of `deprecated conversion from string constant to ‘char*'` warnings in GCC?):
当传递文字而不是其他 const 字符串时,并且您不确定库是否正在修改字符串,很容易在 C++ 中创建一个堆栈分配的文字临时副本(灵感来自如何摆脱字符串常量的弃用转换到 GCC 中的 'char*'` 警告?):
char strMyLogger[]="MyLogger";
g_pCallback->DynamicTag(strMyLogger, &tag);
Use explicit cast to work around a weak library prototype
使用显式转换来解决弱库原型
On most compilers explicit conversions avoid warnings, like:
在大多数编译器上,显式转换避免了警告,例如:
g_pCallback->DynamicTag(const_cast<char *>("MyLogger"), &tag);
Note: You can use this only when you are sure the function is really never modifying the string passed (i.e. when the function could be declared as const char *, but it is not, perhaps because the library writer has forgotten to add it). An attempt to modify a string literal is an undefined behaviour and on many platforms it results in a crash. If you are not sure, you need to make a writeable copy of the string, which may be dynamically allocated or even stack allocated, when you know some upper limit for the string size.
注意:只有当您确定该函数确实从不修改传递的字符串时(即当该函数可以声明为 const char * 时,但事实并非如此,可能是因为库编写者忘记添加它),您才可以使用它。尝试修改字符串文字是一种未定义的行为,在许多平台上它会导致崩溃。如果您不确定,当您知道字符串大小的一些上限时,您需要制作字符串的可写副本,该副本可能是动态分配的,甚至是堆栈分配的。
回答by eran
Given you can't change DynamicTag
, you have to change the way you call it. You can use, for instance:
鉴于你不能改变DynamicTag
,你必须改变你的称呼方式。例如,您可以使用:
char descr[] = "MyLogger";
g_pCallback->DynamicTag(descr, &tag);
Given pDesc is declared as an [in] argument means it would probably not be changed, so you might get away with casting the const away, but that's a bad habit.
鉴于 pDesc 被声明为 [in] 参数意味着它可能不会被更改,因此您可能会逃避将 const 丢弃,但这是一个坏习惯。
回答by iammilind
Pass that value as an array.
将该值作为数组传递。
int tag =0;
char arr[] = "MyLogger";
g_pCallback->DynamicTag(arr, &tag);