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
#define USER "user"
#define PASSWORD "pass"
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. 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*
. I used the specific code segment for simplicity.