C++ operator overloading takes pointer type as parameter?

Deadly 提交于 2019-12-13 14:11:26

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/8245268/c-operator-overloading-takes-pointer-type-as-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!