6.7 Overloading of Operators and Functions

Operator overloading, so popular in C++, is a way to implement polymorphism for some of the unary and binary operators in a language. The overloading operates at two levels. For example, consider the four arithmetic operators +, −, * and /. Even in C language these operators are overloaded in the sense that though for both integers and floats the meaning of ‘+’ operator is “addition of numerical value”, the actual details of the operation, exceptional condition, range of valid results, etc. are quite different for them.

Though not available, we can have further overloading of ‘+’ in some hypothetical language, for concatenation of strings, i.e., “hello” + “world” will produce “hello world”. This is almost ...

Get Compilers: Principles and Practice now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.