Yes, in C ++ you should always refer to string literals with variables like const char * or const char [N] . This is also best practice when writing new C code.
String literals are stored in read-only memory whenever possible; their type is properly const -qualified. C, but not C ++, includes backward compatibility bonuses, where the compiler gives them the type char [N] , even if they are stored in read-only memory. This is because string literals are older than const . const was invented in anticipation of what is now called "C89" - an earlier form of the "K & R" language did not have this.
Some C compilers include an additional mode in which the backward compatible wart is disabled and char *foo = "..."; will provide you with the same or similar diagnostics as in C ++. GCC runs this -Wwrite-strings mode. I highly recommend it for new code; however, including it for old code may require a huge number of tools for very little benefit.
zwol Jul 25 '16 at 17:45 2016-07-25 17:45
source share