You're not allowed to dereference a
char * that points to a string literal, because string literals are ROM (outside an array declaration). Writing to a string literal is not noticed during compilation; thus, the system will throw a run-time exception to indicate a violation of the ROM (Read-Only Memory). For example, here's what's allowed, and what isn't, and why:
1 2
|
char *String("String");
String[0] = '...';
|
This code is unsafe, because "
String" is a pointer to non-constant data, but the string literal it points to is read-only. Attempting to write to this ROM is detected during run-time. So when the time comes to dereference "
String", the system will realise you're attempting to alter data it marked as ROM, and will throw an exception; cue the seg-fault.
|
const char *String("String");
|
This code is safe, because "
String" is a pointer to ROM, which corresponds the const-ness of a string literal. Attempting to dereference "
String" will result in a compile-time error, so there's no chance of the system throwing a run-time exception.
1 2
|
char Array[] = "String";
Array[0] = '...';
|
This code is also safe, because you're not assigning "
Array" to a string literal, although, it does appear as though you are. As a result, writing to "
"Array"" is a safe operation, which both compiler and system respect.
Based on what I said above, your "
STRING" class is unsafe, and prone to run-time errors. Consider
std::string, or dynamically allocate the array.
Wazzak