std::string
is a typedef for a specialization of basic_string
class templated on a char
(objects that represent sequences of characters).
The expression string a = "hello"
corresponds to a stream of characters whose size is allocated statically.
Short, simple and typesafe std::cout
is a typedef of ostream
class templated on standard objects (provides support for high-level output operations).
cout
means "the standard character output device", and the verb <<
means "output the object".
cout << a;
sends the string like a stream to the stdout
.
char *
are special pointers to a constant character, they point to ASCII strings e.g.:
const char * s = "hello world";
The expression (char *)"hello"
corresponds to a char *
pointer, where you are casting away const
, but then the constructor immediately puts the const
back on the call.
cout
, therefore, will print a string because it has a special operator for char *
wich it will treat as a pointer to (the first character of) a C-style string that outputs strings.
char*
or const char*
, cout
will treat the operand as a pointer to (the first character of) a C-style string, and prints the contents of that string: