Public Group

# 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.

## 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 on other sites
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 on other sites
strcpy(m_Name, "Default");

theTroll

##### Share on other sites
Quote:
 Original post by TheTrollstrcpy(m_Name, "Default");theTroll

std::string m_Name = "Default";

##### Share on other sites
#!/usr/bin/pythonname = "Default"

hehe, sorry. I think I'm turning into a Python fanboy.

##### Share on other sites
Quote:
 Original post by RaString 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]

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013598
×