Here "someLetter: "
is a string literal, that is, a const char *
pointer, usually pointing to a read-only region in memory where all string literals are stored.
someLetter
is a char
, so "someLetter: " + someLetter
performs pointer arithmetic and adds the value of someLetter
to the address stored in the pointer. The end result is a pointer that points somewhere behind the string literal that you planned to print.
In your case, it seems that the pointer falls into the character table and points to the second character of the ios::clear
method name. This is completely arbitrary, although the pointer may eventually point to a different (possibly inaccessible) location, depending on the value of someLetter
and the contents of the literal storage area. So this behavior is undefined, you cannot rely on it.
source share