Evan Harper
Evan Harper

Reputation: 430

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?

Upvotes: 9

Views: 779

Answers (3)

Ray Toal
Ray Toal

Reputation: 88458

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".

Upvotes: 12

Cameron
Cameron

Reputation: 98846

It's overloaded in the sense that the same character is used to mean different things in different places (e.g. pointer dereference, multiplication between ints, multiplication with other built-in types, etc.).

Generally, though, "operator overloading" refers to defining an operator (that has the same symbol as a built-in one) using custom code so that it does something interesting with a user defined type.

So... you're both right :-)

Upvotes: 1

Eran Zimmerman Gonen
Eran Zimmerman Gonen

Reputation: 4507

I believe you are. The dereference op and the mult. op are different operators, even if written the same. same goes for +,-,++,--, and any other I may have forgotten.

I believe the book, in this paragraph, refers to the word "overloaded" as "used in more than 1 way", but not by the user. So you could also consider the book as being correct... also depends if you're referring to the overloading of the * operator or of the multiplication operator (for example).

Upvotes: 2

Related Questions