tags:

views:

98

answers:

4

Hi, i have a question about the function call in the following example:

int main()
{
    int a, b;
    cin  >> a >> b >> endl;
    cout << *f(a,b);
    return 0;
}

So is *f(a,b) a valid function call?

Edit:: sorry for the errors, i fixed them now i'm a little tired

+6  A: 

Whatever f is, *f(a, b) attempts to apply the indirection operator to the result of f(a, b).

If f is a function pointer and you're trying to call it, while you could do this:

(*f)(a, b)

Just doing f(a, b) is simpler.

GMan
No f is a function not a pointer to a function...
Vlad
@Vlad: Maybe you should add that to the question so we don't have to try and read your mind, eh? In that case, it applies indirection to whatever `f(a, b)` returns. (And if it cannot do that, your program is ill-formed. That is, `f` needs to either return a pointer or a class-type with the unary `operator*` overloaded.)
GMan
A: 

I don't think there is any way to tell without seeing the definition of f. C++ requires a lot of context to know what is going on.

BigSandwich
More of a comment, I think.
GMan
A: 

It is very tough to tell if it is valid or not without knowing what 'f' is. But if it returns something that can be dereferenced it looks fine as long as that 'dereferenced' value can be printed (an overloaded operator <<) should exist for the type.

Chubsdad
+6  A: 

The code at least could be reasonable. For it to work, f must be defined as a function that returns either of two sorts of things: either it returns a pointer, in which case the * dereferences the pointer, so whatever it was pointing at gets sent to standard output. Otherwise, f must return some user-defined type that defines operator * to return something that's compatible with cout.

Jerry Coffin
Ok so if the function were to return the sum of the numbers the call isn't valid?
Vlad
@Vlad: the answer depends on the *type* used to hold the sum. If it's `int`, `long`, `double` (etc.), then no, not valid. If it's some UDT that defines a (unary) `operator*`, then yes (though the latter seems unlikely).
Jerry Coffin
Ok this answers my question thanks Jerry;)
Vlad