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. (:
These operators can be overloaded globally or on a class-by-class basis. Overloaded operators are implemented as functions and can be member functions or global functions. An overloaded operator is called an operator function. You declare an operator function with the keyword operator preceding the operator.
This feature is present in most of the Object Oriented Languages such as C++ and Java. But C doesn't support this feature not because of OOP, but rather because the compiler doesn't support it (except you can use _Generic).
Rules for Operator Overloading:You can only overload existing operators. You can't overload new operators. Some operators cannot be overloaded using a friend function. However, such operators can be overloaded using member function.
Two operators = and & are already overloaded by default in C++. For example, to copy objects of the same class, we can directly use the = operator. We do not need to create an operator function. Operator overloading cannot change the precedence and associativity of operators.
No, it is not possible. C does not support operator overloading by the developer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With