warning: deprecated conversion from string constant to 'char*'|

Images typdef
typedef std::map<char*, SDL_Surface*> Images;

Code in question.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool LoadResources(Images& images)
{
    images["chess_board"] = IMG_Load("img/chess_board.png");

    images["selector"] = IMG_Load("img/selector.png");

    images["white_pawn"] = IMG_Load("img/white_pawn.png");
    images["white_rook"] = IMG_Load("img/white_rook.png");
    images["white_knight"] = IMG_Load("img/white_knight.png");
    images["white_bishop"] = IMG_Load("img/white_bishop.png");
    images["white_queen"] = IMG_Load("img/white_queen.png");
    images["white_king"] = IMG_Load("img/white_king.png");

    images["black_pawn"] = IMG_Load("img/black_pawn.png");
    images["black_rook"] = IMG_Load("img/black_rook.png");
    images["black_knight"] = IMG_Load("img/black_knight.png");
    images["black_bishop"] = IMG_Load("img/black_bishop.png");
    images["black_queen"] = IMG_Load("img/black_queen.png");
    images["black_king"] = IMG_Load("img/black_king.png");

    return true;
}


gpp's pouting.
warning: deprecated conversion from string constant to 'char*'|


I know I can just shut off this warning, but I prefer to try to be as much of a perfectionist as I can be. :)

So I know I can rid myself of this warning by casting my string constant as a char* but... casting is ugly, especially when unneeded.

I always thought a string constant WAS a char*...

Well its not a real problem but I figured I would post here to let the geniuses have a look.
A string constant is a const char*
BTW std::map won't work well with C style string, why don't you make it typedef std::map<std::string, SDL_Surface*> Images;?
oh right... I always miss the obvious stuff. :/
Thanks.

I didn't use a std::string because it is constant.
Because I will never be adding to it I figured I might as well save the tiny bit of memory and not use a bulkier storage class. Not that the extra space is really a lot or anything.

EDIT:

What problems can a const char* cause in a map?
Last edited on
std::map uses comparison operators to sort the elements and they won't work as expected with C strings ( since you are comparing pointers and not strings )

A std::string doesn't consume really large amounts of memory
good point.

odd thing thought, it has worked fine actually.
Maybe gpp scans and assigns all hard coded C strings the same address. (good thing actually)


Well with that knowledge and hypothesis , to prevent a non hard coded C string from screwing everything up, I'll convert to a std::string.
That is.. if I remember to after this conversation I'm about to have.

:'( ......
It is likely that if you have more than one .cpp file that accesses the map by hard-coded string,
then one of the .cpp files will mysteriously not be able to find the key. This is because the
linker is more than likely not smart enough to fold all the same constants into one across multiple
translation units.
Topic archived. No new replies allowed.