First, string is a class, and char * is a basic data type.
Second, const_cast can only change the const attribute of the pointer, but cannot change or remove its own const attribute.
Test code:
// MemcpyTest. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include" string "using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {string Str =" abcd "; char * pChar = "mmnddd"; // pChar [0] = 'J'; // access conflict exception // char * pstr1 = Str. c_str (); // type exception // const char * pstr1 = Str. c_str (); pstr1 [0] = 'G'; // error char * pStr = const_cast <char *> (Str. c_str (); // remove the const attribute pStr [0] = 'H'; char * pchar1 = const_cast <char *> (pChar ); // remove the const attribute // pchar1 [0] = 'E'; // an access conflict exception occurs, the const attribute cannot be removed. // if you want to remove the const attribute, the heap space must be allocated. char * pchar2 = new char [8]; // pchar2 = pChar; // pchar2 [0] = 'E'; // This does not work either, but the Pointer Points to the same memory area. No one in this area can change memcpy (pchar2, pChar, sizeof (pchar2) * 2); pchar2 [strlen (pChar) + 1] = '\ 0'; pchar2 [0] = 'E'; // return 0 correctly;