What is operator overloading in C++?
Operators for user-defined classes can be made to function in C++. This indicates that the operator overloading feature of C++ allows it to give the operators a special meaning specific to a data type. As an illustration, we can overload the operator "+" in a class like "String" such that we can append two strings with simply the letter "+." The classes Big Integer, Complex Numbers, and Fractional Numbers are a few others where mathematical operators may be overloaded.
Compile-time polymorphisms include operator overloading. It is the concept of providing an existing C++ operator with additional meaning while maintaining its original meaning.
The variables "x" and "y" in this example are of the built-in data types "int" and "float." The contents of "x" and "y" can thus be added simply using the addition operator "+." Because only variables with built-in data types are predefined to be added by the addition operator "+," this is the case.
Now, consider another example
Three variables of type "class B" are used in this example: "b1," "b2," and "b3." Using the "+" operator, we are attempting to combine two objects, "b1" and "b2," that are of a user-defined type, or type "class B." This is prohibited because the addition operator "+" can only operate on built-in data types by default. However, since "class B" is a user-defined type, in this case, the compiler produces an error. In this situation, the idea of "Operator overloading" is relevant.
The "+" operator must now be redefined so that it produces two class objects if the user desires it to add two class objects. Utilizing the idea of "operator overloading," this is accomplished. Thus, the primary principle of "Operator Overloading" is to employ C++ operators with class variables or class objects. Operators' original meaning is not really altered by redefining them; rather, they have been given new meanings in addition to their previous ones.
What distinguishes operator functions from regular functions?
The same rules apply for operator functions as for regular functions. The main distinctions are that an operator function's name always consists of the operator keyword headed by the operator symbol and that operator functions are called whenever the associated operator is used.
Why below operators are not overloaded?
1) typeid - This gives a CPP application the ability to retrieve the true derived type of the object that a pointer or reference is referring to. The sole purpose of this operator is to identify a type in a unique way. Polymorphism can be used to make a user-defined type "appear" like another type, but it is crucial that the typeid operator's meaning not be changed as otherwise grave problems may result.
2) sizeof - This gives the operand's (the object or data type entered) size. The compiler evaluates this; it cannot be evaluated at runtime. The sizeof operation is implicitly used for appropriate pointer incrementation in an array of objects. By changing its meaning through overloading, a foundational aspect of the language would be destroyed.
3) Scope resolution (::) - By providing a namespace, aids in locating and defining the area to which an identifier corresponds. It operates on names instead of values and is entirely assessed at runtime. If CPP were overloaded, there is no syntax for capturing the operands of scope resolution because they are not operations with data types. Therefore, overloading this operator is illogical from a syntax perspective.
Important information on operator overload