Home > Article > Backend Development > Can You Overload Operators for Built-in Types like int and float in C ?
Operator Overloading in C
In C , it's possible to redefine operators to alter their behavior with custom types. However, a common question arises:
Can we overload operators for built-in numerical types like int or float?
The answer is no.
Operator overloading allows for language extension but not modification of existing built-in types. To overload an operator, at least one parameter must be of a user-defined type or a reference to it.
In the example provided:
<code class="cpp">int operator + (int, int);</code>
Both parameters are of the built-in type int. Since no user-defined types are involved, this operator declaration is invalid.
Therefore, it's not possible to overload operators for fundamental types like int or float in C .
The above is the detailed content of Can You Overload Operators for Built-in Types like int and float in C ?. For more information, please follow other related articles on the PHP Chinese website!