Dnia Sat, 17 Mar 2007 23:47:02 -0700, arnuld napisał(a):
So.. tell me this:
"foo()" is a call for the function. does it do ask for the address ?
When the compiler sees the following:
foo;
he have to know first what the name "foo" means.
If he know it's a function name, he treats that "foo"
as an address of that function. So in this expression statement
the compiler evaluate that address and... do nothing with it
[will forget it when he come to the ending semicolon].
So it warns you that maybe you do something by an error.
On the other hand, when the compiler sees the following:
foo(7);
and it knows that "foo" is a name of a function, it does the
following:
1. evaluates the "foo" as an address of a function "foo".
2. evaluates "7" as an literal "in-place" value.
3. sees that parentheses, so evaluates it as a function-call
operator. So it calls a function from the address evaluated
from "foo" with a parameter being literal value "7".
4. When the function returns, the returned value replaces this
whole expression. That value is a temporary object. It isn't
stored anywhere, so it'll be forgotten soon.
5. When the program execution comes to the ending semicolon, it's
the end of a statement, so all the temporary objects are
destructed. The value of a function call is forgotten.
So, the "foo" alone means "address of a function 'foo'".
The "foo" followed by parentheses means "call of a function
'foo'". But "*foo" means referring to the memory location
referring to by the address of function "foo".
you meant "foo;" is same as "*foo";
with pointer, i mean i am taking the address of function "foo".
"foo" is the same as "&foo" for a function name.
Operator * takes the address as an argument, but the result
it evaulates to is the object [memory location] pointed to
by that address. If you have:
int a = 8;
int* p = &a;
you get:
p a
[0xDEADBEEF]------------->[ 8 ]
Now, "p" is a pointer, so evaluates to an address.
"*p" evaluates exactly as a memory location containing value "8",
so the result will be that value. So in this case "*p" is the
same as "a".
std::cout << a << ' ' << p << ' ' << *p << std::endl;
would print out:
8 0xDEADBEEF 8
--
SasQ