Jump to content
  • Advertisement
Sign in to follow this  
sipickles

const char* conundrum

This topic is 4837 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

Hi, Can anyone help me change a const char* to a char* and vice versa? SOme functions require one and some the other! I am using Cstring to store text, but then the drawtext function won't accept the return from cstring->c_str() Any ideas? Thanks Simon

Share this post


Link to post
Share on other sites
Advertisement
Passing a char* to a function expecting a const char* → nothing to do. A function taking a const char* promises not to modify the data the pointer points to, that is all.

Passing a const char* to a function expecting a char* will require either you making a copy of the data that the function can modify (a function taking a char* does not promise not to change the data, const char* data cannot be changed, so const char* cannot be passed where a char* is expected), or, in some very specific circumstances, the const-ness can be cast away.

Quote:
I am using Cstring to store text, but then the drawtext function won't accept the return from cstring->c_str()


If you know for sure that the drawtext function really is not going to modify the string data you pass it, then you may do a const_cast: const_cast<char*>(cstring->c_str()). The main problem is that if drawtext really changes the text, it will cause trouble in the CString class. For example, if you change the length of the string, but CString may keep track of that length independently. Since you were not supposed to modify the data, the length that the CString think your string has now does not match its real length... Oops.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!