Quote: error C2664: 'Log' : cannot convert parameter 1 from 'const char *' to 'char *' Conversion loses qualifiersThe problem is that my Log function takes as argument a 'char*' and I want to pass a std string. I'm trying this but it doesn't work: string Message; Log(Message.c_str()); Can someone point how this should be done?
'const char *' to 'char *'
I'm trying to build a Log file but I keep getting this error:
Do you mean my method shold work or I should pass something else in my function?
If the later how do I convert the Message.c_str() to a const char*. I googled for
'const char *' to 'char *' but nothing helpful came up.
If the later how do I convert the Message.c_str() to a const char*. I googled for
'const char *' to 'char *' but nothing helpful came up.
He means you should re-write your function so its parameter is const char*. And he is quite right.
Basically your Log() function should take a const char* as it doesn't modify the value of the variable that you're passing (or at least it shouldn't).
Every function that doesn't change a pointer should usually define the parameter as const, this way you'll know for sure that your pointer isn't being modified (in theory at least).
Basically just change
Every function that doesn't change a pointer should usually define the parameter as const, this way you'll know for sure that your pointer isn't being modified (in theory at least).
Basically just change
void Log( char* );
tovoid Log( const char* );
Quote:Original post by MHOOO
Basically your Log() function should take a const char* as it doesn't modify the value of the variable that you're passing (or at least it shouldn't).
Every function that doesn't change a pointer should usually define the parameter as const, this way you'll know for sure that your pointer isn't being modified (in theory at least).
I guess what you were trying to say is correct, but the wording is very wrong. It should read like this:
Basically your Log() function should take a const char* as it doesn't modify the value of the data referenced by the variable that you're passing (or at least it shouldn't).
Every function that doesn't change the data referenced by a pointer should usually define the parameter as pointer to const, this way you'll know for sure that the data referenced by your pointer isn't being modified (in theory at least).
say if the function did need to edit the data referenced by the pointer he could do something like this
std::string Message;char *tmp = strdup(Message.c_str());Log(tmp);//do something with the modified string datafree(tmp);
If you absolutely must do that conversion use a const_cast:
Of coorse this cast is generally not recommended and indicates poor programming.
const char* const cc = "Hello!";char* c = const_cast<char*>(cc);
Of coorse this cast is generally not recommended and indicates poor programming.
Quote:Original post by Magos
If you absolutely must do that conversion use a const_cast:
*** Source Snippet Removed ***
Of coorse this cast is generally not recommended and indicates poor programming.
... or the need to adapt to some old pre-existing code [smile]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement