Terry Li
Terry Li

Reputation: 17268

C++ operator overloading takes pointer type as parameter?

I'm new to C++ and trying to figure out the differences between pointer and reference. I've just read this short summary.

In the article, the author mentioned that day *operator++ (day *d); won't compile (note: day is an enum type) and argued that the parameter for this overloaded operator function must be type T, T&, or T const&, where T is a class or enum type.

I assume that pointer is a built-in type rather than a class or enum so it can't be used to overload operators and that operator overloading is not possible for all built-in types such as int and double.

For example, int i = 1; ++i; would never result in i being 3 by overloading the ++ operator for the type int.

Am I correct? Please help me understand this problem better.

Upvotes: 3

Views: 3911

Answers (2)

Viruzzo
Viruzzo

Reputation: 3025

Yes, pointer are primitive types and not objects. They are just numbers (the memory address of the object they point to), and as such arithmetics can be applied to them.

Yes, you cannot overload operators for primitive types (you can however overload binary operators in a class that take a primitive type parameter).

Upvotes: 4

Alok Save
Alok Save

Reputation: 206546

First rule in Operator overloading is:
You cannot overload operators for built-in data types, You can only for your custom data types, So you are correct in that regard.

Upvotes: 5

Related Questions