Home > Backend Development > C++ > body text

Can we Overload Operators for Built-in Types like `int` or `float` in C ?

Mary-Kate Olsen
Release: 2024-10-28 07:38:29
Original
849 people have browsed it

Can we Overload Operators for Built-in Types like `int` or `float` in C  ?

Operator Overloading for Built-in Types

Operator overloading is a powerful feature that allows us to extend the functionality of built-in operators to our own custom types. However, a common question arises: can we use operator overloading to redefine operators for built-in types like int or float?

Question:

Can we declare a function like the following in C :

<code class="cpp">int operator + (int, int);</code>
Copy after login

Answer:

No, we cannot redefine a built-in operator for a built-in type. Operator overloading is designed to allow us to extend the language with new capabilities, rather than alter the existing ones. Therefore, at least one of the parameters of an overloaded operator must be either a user-defined type (class or enum) or a reference to one.

The above is the detailed content of Can we Overload Operators for Built-in Types like `int` or `float` in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template