-
Cast aways const
Problem :
PHP Code:
void Menu::DrawMenu(string sCaption)
{
char * cTempo = sCaption.c_str();
// int, int, char*, WORD
VideoXY(x,0, cTempo ,YELLOW | BLIGHTBLUE);
}//End DrawMenu
I want to put the string (sCaption )to replace the char*(cTempo)
BUT I got that message :
Code:
error C2440: 'initializing' : cannot convert from 'const char *' to 'char *'
Conversion loses qualifiers
How can I transform the string to char* ???
:)
-
Here is what I have thought but it doesn't work.
to but a String to a char*
myString.c_str()
But I have that error now : Cannot convert parameter 3 from 'const char *' to 'char *' Conversion loses qualifiers
Anyone can help me?
-
Try something like this:
PHP Code:
char * cTempo = (char8)sCaption.c_str();
-
No need, the function should accept a const char*:
Code:
void Menu::DrawMenu(string sCaption) {
// int, int, char*, WORD
VideoXY(x, 0, sCaption.c_str(), YELLOW | BLIGHTBLUE);
}//End DrawMenu
Actually, I would suggest using const string &sCaption for performance (saves copying the string when you pass it in, and it lets the compiler inline better in some situations).
-
VideoXY(x, 0, sCaption.c_str(), YELLOW | BLIGHTBLUE);
doesnt work! I have a constant problem :confused:
-
Where is the VideoXY function declared? Is it one of yours? If so, add the 'const' to the prototype if it doesn't change the source string.
You should *always* put const on something if it doesn't need to change it, to allow it to be used in a greater number of circumstances.
-
1 Attachment(s)
-
Where does the initial function come from? Did you write it or is it API?
-
I write it but you neeed the LPSTR at the end for the API that call SetPosXYandColor.
DrawMenu use : VIDEOXY who use : SetPosXYandColor who use API
*APi need LPSTR
Daok
-
Which API function are you using?
-
Quote:
Originally posted by abdul
Try something like this:
PHP Code:
char * cTempo = (char8)sCaption.c_str();
well now it work with something like that :
PHP Code:
void Menu::DrawMenu(string sCaption) {
// int, int, char*, WORD
VideoXY(x, 0,(char*) (sCaption.c_str()), YELLOW | BLIGHTBLUE);
}//End DrawMenu
-
Quote:
Originally posted by MasterDaok
well now it work with something like that :
PHP Code:
void Menu::DrawMenu(string sCaption) {
// int, int, char*, WORD
VideoXY(x, 0,(char*) (sCaption.c_str()), YELLOW | BLIGHTBLUE);
}//End DrawMenu
Ops, ya that "8" should really be "*".
-
Nooooo.....evil evil evil! Pass by reference!
Then use const_cast<> ;)
-
const_cast<> will do the same thing?
-
For example:
Code:
void bad_code(char *str) {
char b;
while(*str++) {
b = *str;
}
}
void my_code() {
const char *c = "Hello there!";
// bad_code(c); // won't compile
bad_code(const_cast<char*>(c));
}
You can mostly get away with it because it's not trying to change it. Nasty things could happen in a ROM environment though :S
-
Thx it work well that way too :)