Jump to content
  • Advertisement
Sign in to follow this  
Elspin

Constant to unconstant

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

So I have a class that has a private member defined like this:
char m_Name[30];
And in the constructor has this in it
m_Name = "Default";
Then, it produces this compiler error:
1>c:\documents and settings\spencer\my documents\visual studio 2005\projects\learning\learning\base_entity.h(30) : error C2440: '=' : cannot convert from 'const char [25]' to 'char [30]'
I tried changing it to
m_Name = (char) "Default";
To remove the const in a type cast, but it just told me the type needs to be char[30] instead of char, and I can't type-cast arrays as far as I know. So what's the solution to this very annoying problem?

Share this post


Link to post
Share on other sites
Advertisement
String literals have the type char const *. In other words, they're pointers to constant characters. This is not the same as an array, especially not one with a fixed size.

char const * m_Name; (or const char * m_Name; if you prefer) will fix this.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ra
String literals have the type char const *. In other words, they're pointers to constant characters. This is not the same as an array, especially not one with a fixed size.

char const * m_Name; (or const char * m_Name; if you prefer) will fix this.



I don't understand what you mean here, can you please explain? Which part would I be replacing with this? Are you saying m_Name should never be a char array, but rather just a pointer?

EDIT:
Nevermind, I figured it out. Works well, thanks :)

[Edited by - Elspin on September 28, 2007 11:04:20 PM]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!