Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

neurokaotix

Strange std::map issue involving char*

This topic is 5331 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m having a bit of trouble here with std::map and I hope someone can give me an explanation. To sum up the problem, I''m storing pointers in a map and using a char* name as the reference to them. I can use text incapsulated in quotes as the first parameter and I''m able to retrieve the pointer just fine, but I cannot use a char* variable with the name stored in it. Let me post some code for clarification:
std::map<char*, mFileSystemInterface*> filesystems;
        
//this works:

this->filesystems.insert(std::make_pair((char*)"zip", zipfilesystem));
        
//this does not work:

char* MyName = "zip"
this->filesystems.insert(std::make_pair(MyName, zipfilesystem));
Here is my pointer retrieving code:
std::map<char*, mFileSystemInterface*>::iterator it;
it = filesystems.find(fsname);
if (it == filesystems.end())
{ 
        //It couldn''t find the pointer

        mLog::l->Write("%s didn''t work", fsname);
        it = filesystems.find("stdio");
        return it->second;
	
}
else
{
        //It found the pointer

        mLog::l->Write("%s worked", fsname);
        return it->second;
}
For some odd reason I can retrieve the pointer if I added the name via (char*)"zip", but if I add it with MyName it just refuses to work. Does anyone know why this is? MindEngine Development http://medev.sourceforge.net

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You should generally not use char* as key to the map. Use std::string instead. The map uses operators like > and == to compare the keys; and for char*, they just compare the pointers and not the content.

Share this post


Link to post
Share on other sites
Mostly using char * won''t do what you want. For example comparison will usually be done using the == operator, which won''t work for C-style strings.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!