At 3/14/13 08:38 PM, Momo-the-Monkey wrote:
Why doesn't the assignment in the second or the reference in the third line need the * like regular pointers? Thanks in advance.
Because you only need to use * when you are declaring a pointer, not when you are assigning a value.
When you are doing:
char *s;
You're declaring a char pointer with the name "s", and when you do:
s = "somestring";
The compiler already knows that "s" is a char pointer, so it will accept any char pointer as an r-value for the assignment operator, which is precisely what is being given.
At 3/14/13 08:38 PM, Momo-the-Monkey wrote:
cout << s;
This is actually a function, not an assignment; cout is just a variable, and is an instantiation of the ostream class.
In that example cout is using << as an overloaded operator like this:
void ostream::operator << (const char *str) {
// Do fancy stuff with the string
}
So when you use << on cout that function ends up being called, and the r-value is passed to that function.