Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the C++ * operator "already overloaded?"

My C++ teacher thinks that the * operator in standard C++ is "already overloaded," because it can mean indirection or multiplication depending on the context. He got this from C++ Primer Plus, which states:

Actually, many C++ (and C) operators already are overloaded. For example, the * operator, when applied to an address, yields the value stored at that address. But applying * to two numbers yields the product of the values. C++ uses the number and type of operands to decide which action to take. (pg 502, 5th ed)

At least one other textbook says much the same. So far as I can tell, this is not true; unary * is a different operator from binary *, and the mechanism by which the compiler disambiguates them has nothing to do with operator overloading.

Who is right?

like image 546
Evan Harper Avatar asked Aug 23 '11 13:08

Evan Harper


1 Answers

Both are right as the question depends on context and the meaning of the word overloading.

"Overloading" can take a common meaning of "same symbol, different meaning" and allow all uses of "*" including indirection and multiplication, and any user-defined behavior.

"Overloading" can be used to apply to C++'s official operator overloading functionality, in which case indirection and multiplication are indeed different.

ADDENDUM: See Steve's comment below, on "operator overoading" versues "token overloading".

like image 155
Ray Toal Avatar answered Sep 22 '22 18:09

Ray Toal