Is it possible to overload operators in C?

青春壹個敷衍的年華 提交于 2019-11-28 03:13:23

问题


Is it possible to overload operators (such as operators of comparison) in C?

If so, how do you do it? I did a quick search, but all I found was for C++, and what I want is for C.

Anyone have any ideas?

Edit1: The idea is: I have a struct, and I need to do a comparison (based on a member of the struct). And for this I would like to associate operators compared to my new "data type".

Edit2: I am completely aware that I can do without the use of operator overloading, but was wondering if you can do this WITH OVERLOAD.

Answer: The concept of overload is associated with object-oriented programming. Since C is not object oriented and therefore can not contain a concept of overload. (:


回答1:


No, it is not possible. C does not support operator overloading.




回答2:


If by overload, you mean user defined operator overloads, then the answer is no. However, some of the predefined operators such as *, + etc. are overloaded (if you think about it) for arithmetic types. The * is special since it also has an overload for de-referencing pointers.




回答3:


C does not support overloading of operators or functions. There's no way you can redefine <, <=, >, >=, ==, or != to compare struct types directly.



来源:https://stackoverflow.com/questions/10624525/is-it-possible-to-overload-operators-in-c

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