简体   繁体   English

消除从字符串常量到'char *'的弃用转换的最佳方法”

[英]Best way to eliminate deprecated conversion from string constant to ‘char*’"

I have came across a situation as follows and I am getting following warning from the compiler. 我遇到了以下情况,并且正在从编译器获取以下警告。

    Main.cpp:14:22: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
   Login(USER,PASSWORD);

Here are the codes Passwords.h 这是密码Passwords.h

#define USER "user"
#define PASSWORD "pass"

Main.cpp Main.cpp

#include <iostream>
#include "Passwords.h"

using namespace std;

void Login(char* username,char* password)
{
  cout << "UserName is " << username <<endl;
  cout << "Password is " << password <<endl;
}

int main()
{
  Login(USER,PASSWORD);
  return 0;
}

One thing I decided to do was to introduce a global variables in the Passwords.h file. 我决定要做的一件事是在Passwords.h文件中引入一个全局变量。 But I really like to know what is the best practice to solve this issue. 但是我真的很想知道解决这个问题的最佳实践。

I hope no one will mark this as duplicate as same question is asked. 我希望没有人会在提出相同问题时将其标记为重复。 I need to eliminate this warning in legitimate way and looking for a best practice as many answers for the same problem gave some hacks to turn off compiler warnings and some casting solutions. 我需要以合法的方式消除此警告,并寻求最佳实践,因为针对同一问题的许多答案都给了一些黑客以关闭编译器警告和某些强制转换解决方案的机会。

Update 更新资料

The Login function is actually a virtual function so the parameter datatypes cannot be changed from char * to const char* . Login函数实际上是一个虚拟函数,因此无法将参数数据类型从char *更改为const char* I used the specific code segment for simplicity. 为了简单起见,我使用了特定的代码段。

But I really like to know what is the best practice to solve this issue. 但是我真的很想知道解决这个问题的最佳实践。

The best practice is to use pointers/references to const when no modification is made to the pointed object/array. 最佳实践是在未对指向对象/数组进行任何修改的情况下,使用指向const的指针/引用。 You should make the following change to the parameters: 您应该对参数进行以下更改:

void Login(const char* username, const char* password)

Conversion from string literal to const char* is well-formed and not deprecated. 从字符串文字到const char*转换格式正确,不建议使用。


the parameter datatypes cannot be changed from char * to const char* 参数数据类型不能从char *更改为const char*

Your requirement excludes the best practice shown above. 您的要求不包括上面显示的最佳实践。 Given this restriction, you should not pass a string literal to the function. 鉴于此限制,您不应将字符串文字传递给函数。 The options left are workarounds. 剩下的选项是解决方法。 The workaround that I would suggest is to create a local copy of the literal, and pass that instead: 我建议的解决方法是创建文字的本地副本,然后传递该副本:

char user[] = USER;
char pass[] = PASSWORD;
Login(user,pass);

PS. PS。 Since C++11 the conversion from string literal to char* is not only deprecated, but ill-formed instead. 从C ++ 11开始,从字符串文字到char*的转换不仅被弃用,而且格式不正确。 A compiler conforming to the current standard may refuse to compile the program. 符合当前标准的编译器可能会拒绝编译该程序。

PPS. PPS。 As pointed out in the comments, storing a password within the executable as plain text (which is the way string literals are stored) is dubious from security perspective. 如注释中所指出的那样,从安全角度来看,将密码以纯文本形式存储在可执行文件中(这是字符串文字的存储方式)是可疑的。

Given that you can't change the function signature to something more appropriate, you're stuck. 鉴于您无法将函数签名更改为更合适的名称,因此陷入了困境。 Bad code forces more bad code. 错误的代码会强制执行更多错误的代码。

In this case you need to copy the strings to something that isn't const . 在这种情况下,您需要将字符串复制到非const

int main()
{
  char user[] = USER;
  char password[] = PASSWORD;
  Login(user,password);
  return 0;
}

Just do 做就是了

void Login(const char* username, const char* password)

While you are on it, replace define with constexpr . 在使用它时,将define替换为constexpr

暂无
暂无

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

相关问题 从字符串常量到&#39;char *&#39;的错误不建议使用的转换 - ERROR deprecated conversion from string constant to 'char*' 从字符串常量到&#39;char *&#39;的不推荐使用转换:不能使用常量 - Deprecated conversion from string constant to 'char*': Can't use the constant 在CUDA分配模板函数中不建议将字符串常量转换为&#39;char *&#39; - Deprecated conversion from string constant to 'char *' in CUDA allocation template function C ++不推荐将字符串常量转换为'char *' - C++ deprecated conversion from string constant to 'char*' C ++ - 从字符串常量到char的不推荐的转换 - C++ - Deprecated conversion from string constant to char 从字符串常量到&#39;char *&#39;[-Wwrite-strings]的弃用转换 - deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] 如何避免在C ++中不推荐将字符串常量转换为&#39;char *&#39; - How to avoid deprecated conversion from string constant to 'char*' in C++ 使用typedef摆脱GCC中从字符串常量到&#39;char *&#39;的过时转换警告? - get rid of `deprecated conversion from string constant to ‘char*’` warnings in GCC using typedefs? C ++警告:不建议将字符串常量转换为&#39;char *&#39;[-Wwrite-strings] - C++ warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] 如何在C ++中修复从字符串常量到&#39;char *&#39;的警告弃用转换? - How can I fix warning deprecated conversion from string constant to 'char*' in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM