Results 1 to 16 of 16

Thread: Cast aways const

  1. #1

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362

    Cast aways const

    Problem :

    PHP Code:
    void Menu::DrawMenu(string sCaption)
    {
        
    char cTempo sCaption.c_str();
                     
    //         int,  int, char*, WORD
        
    VideoXY(x,0cTempo ,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* ???

  2. #2

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    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?

  3. #3
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Try something like this:
    PHP Code:
    char cTempo = (char8)sCaption.c_str(); 
    Baaaaaaaaah

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    VideoXY(x, 0, sCaption.c_str(), YELLOW | BLIGHTBLUE);

    doesnt work! I have a constant problem

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Here is a screenshot that may help us :

    Attached Images Attached Images  

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Where does the initial function come from? Did you write it or is it API?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    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

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Which API function are you using?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    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(x0,(char*) (sCaption.c_str()), YELLOW BLIGHTBLUE);
    }
    //End DrawMenu 

  12. #12
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Originally posted by MasterDaok



    well now it work with something like that :
    PHP Code:
    void Menu::DrawMenu(string sCaption) {
                     
    //         int,  int, char*, WORD
        
    VideoXY(x0,(char*) (sCaption.c_str()), YELLOW BLIGHTBLUE);
    }
    //End DrawMenu 
    Ops, ya that "8" should really be "*".
    Baaaaaaaaah

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Nooooo.....evil evil evil! Pass by reference!

    Then use const_cast<>
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    const_cast<> will do the same thing?

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  16. #16

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Thx it work well that way too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width