Suppose the return value from strstr was char* , with the first parameter const char* , as in C. Then you can write:
const char *s = "hello, world"; strstr(s, "hello")[0] = 'j';
The code will compile and run (with undefined behavior), but this is the type of error that const was specifically designed to prevent. You converted const char* to char* without a cast.
C can do nothing about it: if strstr returned const char* , then you would need to drop it back to non-constant explicitly if the input is not const and you want to change the line. Since C ++ has an overload function, it can (and does) connect a loophole and make both cases work correctly. Therefore, in C ++, the above code does not compile, and your sample code does too.
source share