Operator Overloading

Source: How to C++ by Deitel & Deitel More notes available at:

‡ Operator overloading is the process of enabling existing C++¶s operators to work with class objects. Contributes to C++¶s extensibility Examples:
± << bitwise left-shift operator and stream insertion operator ± >> bitwise right-shift operator and stream extraction operator ± + & - overloaded to perform differently depending on their context in integer, floating-point arithmetic and pointer arithmetic



In C++, most operators can be overloaded to be sensitive to the context in which they are used. The compiler generates the appropriate code based on the manner in which the operator is used.Compiler can distinguish between overloaded functions by looking at the data type of their arguments. Operator overloading has clearer notion than corresponding explicit functions calls Avoid excessive or inconsistent use of operator overloading as this can make program cryptic and difficult to program Use when usage is obvious ± similar meaning & same syntax.

‡ ‡

that operator must be overloaded ± with two exceptions Operators µ=µ and µ&¶ can be used with any class without explicit overloading. The default behaviour of µ=µ is member-wise assignment and µ&¶ is used to return address of the object in memory. It is operator rich language ± built-in & those defined to work with userdefined classes. ‡ . Prototype: Return type operator<operator-to-be-overloaded>(parameter list) { «} type if not µvoid¶ then ± Create a temp object (if object to be returned) to use as return value. ± Use nameless initialized temp object (return counter(count)) ± Return original object using µthis¶ ‡ ‡ To use an operator on class objects.Introduction ± Contd« ‡ C++ is type-focus and type-sensitive.

For e. Operator overloading functions can be ± member functions.g. Avoid nonintuitive use of operators Before overloading consult the manuals for compiler restrictions & requirements unique to particular objects. a complex number class must have other operators besides addition. friend functions & rarely.Introduction Contd« ‡ Overloading is most appropriate for mathematical classes. Choice of operator overloading depends on meaning & context of each operator. These often require that a substantial set of operators be overloaded to ensure consistency with the way these mathematical classes are handled in the real world. non-member nonfriend functions Overload operators to perform the same function or similar functions on the class objects as the operators perform on objects of built-in types. ‡ ‡ ‡ ‡ .

order of evaluation can be changed using paranthesis ± Associativity ± Arity: Operators (&. Attempting to modify how an operator works with objects of built-in types is a syntax error.Restrictions On Operator Overloading ‡ Following operators cannot be overloaded: ± ± ± ± . Binary operator cannot be overloaded to be unary ‡ ‡ .*.* :: ?: Sizeof ‡ Attempting to overload a non-overloadable operator is syntax error. ‡ ‡ It is not possible to create new operators. Attempting to do so is a syntax error. only existing operators can be overloaded. Operator overloading cannot change operator¶s: ± Precedence: However. thse can be overloaded separately. Therefore.+.-) have unary & binary versions. ** can be overloaded for exponentiation.

Operators can be overloaded only explicitly. Atleast one argument of an operator function must ne class object or a reference to a class object. there is no implicit overloading To ensure consistency among related operators. ‡ ‡ ‡ . use one to implement the others (ie use an overloaded µ+¶ operator to implement an overloaded µ+=µ operator). This prevents programmers from chnaging how operators work on built-in types Assuming that overloading an operator such as µ+¶ overloads related operators such as µ+=µ or that overloading µ==µ overloads µ!=µ is error.Restrictions On Operator Overloading « ‡ Operator overloading works only with objects of userdefined types or with a mixture of an object of a userdefined type and an object of a built-in type.

[]. the operator overloading function must be declared as class member. the operator overloading functions can be non-member functions When an operator function is implemented as a member function (the leftmost (or only) operand must be a class (or a reference to a class object) of the operators¶ class ‡ ‡ ‡ ‡ ‡ .Operator Functions as Class Members Vs Friend Functions ‡ Non member functions are made friends for performance reasons. member functions use µthis¶ pointer implicitly to obtain one of their class object arguments (the left argument for binary operator) In case of non-member functions. . For other operator. In case of binary operators. or any assignment operators. both class arguments must be explicitly listed in call When overloading ().

g. this operator function must be implemented as a non-member function. µ<<µ or µ>>¶ A non-member operator function needs to be friend if that function must access private or protected members of that class directly If non-member functions used for operator overloading are not friend functions then set/get functions may be used.g. so these functions can be inlined to improve performance. The overhead of calling these functions could cause poor performance. left operand must be object of that class.Operator Functions as Class Members Vs Friend Functions « ‡ If the left operand must be an object of a different class or built-in type. to make it commutative. For e. ‡ ‡ ‡ ‡ . overload the operator as a non-member friend function to allow HugeInteger to appear on the right of the addition. So. Operator member functions of a specific class are called only when the left operand of a binary operator is specifically an object of that class. if we overload µ+¶ as member function ten for using it with µlong int class object Huge integer Object¶. For e. or when the angle operand of a unary operator is an object of that class Use non-member operator overloading function to enable the operator to be commutative.

g.Overloading Stream Insertion & Stream Extraction Operator ‡ These operators are overloaded (in class libraries provided with C++ compilers) to process each built-in data type including C-like char* string & Pointers They can also be overloaded to perform i/o operations for user defined types E. ‡ ‡ ‡ ‡ . of overloading >> & << for PhoneNumber class // Why not member function These operators must be non-members because object of class appears in each case as the right operand must appear on the left of the operator to overload that operator as a member function.

}.g. that argument must be either an object of the class or a reference to an object of the class.operator!() is invoked Class string { public: bool operator !() const.Overloading Unary Operators ‡ A unary operator for a class can be overloaded as a non-static member function with no arguments or as a non-member function with one argument. ± Operator is used on the object of which it is a member ‡ ‡ ‡ . «. Non-static so that they can access non-static data members because static member functions can only access static data members of the class E. overloading ! Operator for class string to check if empty & return a bool result Case 1: Non-static member function with no arguments ± !s is converted into s.

Friend function and friend classes must be avoided unless they are absolutely necessary. The friend usage violates the use of encapsulation of a class.Overloading Unary Operators « ‡ Case 2:Non-member function with one argument (Object copy / Object reference) !s operator!(s) class string{ friend bool operator(const string &) « }. ‡ ‡ . When overloading unary operator it is preferable to make the operator function class member instead of non-member friend function.

Overloading Binary Operators ‡ The argument on the LHS of operator is the object of which operator is a member function. Non-member function with two arguments (one of them needs to be either a class object or a reference to a class object) y+=z operator+=(y. const string &). The object on the RHS must be furnished as an argument to operator. ‡ ‡ . z) friend const string &operator +=(string &. Overloaded operator always require on less argument than its number of operands. Overloading as non-static member function with one argument y+=z operator+=(z) const string &operator +=(const string &). Returned value may be assigned or used in any other ways.

they can be characters. arrays ‡ For arrays. //array example Subscript operator [] can be used to select elements from container classes like linked lists. ± One array cannot be assigned to another with assignment operator (s) (because array names are const pointers & a const pointer cannot be used on LHS of assignment operator. ‡ But C++ does provide means to implement these capabilities through the mechanisms of operator overloading. the size of the array must be passed as an additional argument.Case Study: An Array Class ‡ Arrays in C++ just an alternative to pointers have much potential for errors. so. subscripts no longer have to be integer. Also. strings. strings. floats or even objects of user-defined classes ‡ ‡ . dictionaries etc. C++ does not provide such capabilities like ± Subscript range checking ± Two arrays comparison with equality/relational operators ± When an array is passed to a general purpose function designed to handle arrays of any size.

a situation called a ³dangling pointer´ & likely to result in serious run-time error. Otherwise. ‡ ‡ . Not providing an overloaded assignment operator and a copy constructor for such a class is a syntax error. the copy constructor call results in infinite recursion (a fatal logical error) because for callby-value a copy of the object passed to the copy constructor must be made. a destructor. an overloaded assignment operator and a copy constructor usually provided as a group of any class that uses dynamically allocated memory. A copy constructor is used to initialize an object with another object of same class«(to be completed from chapter highlights) If the copy constructor simply copied the pointer in the source object to the target object¶s pointer. which results in copy constructor being called recursively. A constructor. then both objects would point to the same dynamically allocated storage. The first destructor to execute would then delete the dynamically allocated storage & the other object¶s pointer would then be undefined.Case Study: An Array Class « ‡ Copy constructor must use call-by-reference not callby-value.

Making overloaded assignment operator & copy constructor private prevents class objects from being copied Self assignment of objects is dangerous because « Multiple Overloading: If in same program with multiple classes having same operator being overloaded then C++ can select correct function to carry out ³addition´ based on type of operand. ‡ ‡ ‡ .Case Study: An Array Class « ‡ Declaring assignment operator as private prevents one class object being assigned to another.

conversion constructors are needed ± single argument constructor that turn objects of other types (including built-in types) into objects of a particular (constructor¶s) class.Converting between Types ‡ Conversion is required in assignments. This constructor is called implicitly by the constructor. Compilers know how to perform certain conversions among built-in types. A constructor/cast operator converts an object of one class into an object of another class or into an object of a built-in type. For conversion among user-defined types & built-in types. in calculations. //Assignment operator or overloaded role from CHHG This operator must be a non-static member function It cannot be a friend function It does not specify a return type ± type to which an object is being converted Prototype for cast operator: CastFromClass::operator const ‡ ‡ ‡ ‡ ‡ ‡ ‡ . Can also convert if userdefined objects are same on both sides. in passing values to functions & in returning values from functions. This implicit conversion is done through built-in routines.

g. a conversion function is required ± Mtrs=float(dist2) and mtrs = dist2 will use same conversion function ‡ ‡ ‡ . A::operator char *() const. cout << s is converted by compiler to first (char *) as required by cout so for class string << need not be overloaded for cout For converting a basic type into user-defined type.345. (char *)s=s. Distance d1 = 2. the compiler can call cast operators and conversion constructors to create temporary objects ± E. Complier blurs distinction between definition & assignment. d1 = 1.0.operator char*() ‡ When necessary. for object µs¶ of class string.g.Converting between Types « ‡ Prototype for cast operator: CastFromClass::operator const ± E. one argument constructor is used. Constructor is used but no new object is created (through unnamed object) For converting a user-defined type to basic. If no overloaded operator available then compiler will look for the constructor to do the same job.

single argument constructor is destination object & conversion function in source object.Converting between Types « ‡ For converting from user-define to another user-defined type then if conversion routine to be in source object. ‡ . use conversion function else (conversion routine to be in destination object) use one-argument constructor. In second case. special functions (get functions) may be required to provide access to private data members of object to be converted Avoid doing same conversion in more than one way i.e.

Prototype friend Date operator++(Date &) Post-incrementing Member function: d1++ = d1. Prototype Date operator ++(int) Non-member function: d1 ++= d1.0). achieved by making argument list for postfix version.int) ‡ ‡ ‡ .operator(0). each operator overloading function must have a distinct signature so the compiler will be able to determine which versions of ++/-. Example declarations: Date d1.operator(). Postfix versions are overloaded in a manner to have different signature. Prototype friend Date operator++(Date &.operator(d1. So.is intended Prefix versions are overloaded exactly as any other prefix unary operator would be. Pre-incrementing Member function: ++d1 = d1.Overloading ++ and -‡ ‡ These operators have both prefix and postfix versions. Prototype Date operator ++() Non-member function: ++d1 = operator(d1).

c1 is first assigned to c2 and then incremented .Overloading ++ and -.« ‡ Similar is the case for pre-and post decrementing It is not an argument but a signal to compiler to use postfix notation. c2 = c1++.

) Scope resolution (::) Conditional Operator (?:) Pointer to member (.*) sizeof() .Operators that cannot be overloaded ‡ ‡ ‡ ‡ ‡ Member access dot operator (.

4. Add English distances: Add X1 to X2 & Y1 to Y2 Add Polar Co-ordinates: 3 step approach convert to English. 5. 3. 2. Convert back String Concatenation: Check final string should not result in overflow Distance & String comparison Polar to rectangular conversion .Some uses of operator overlaoding 1. Add.

cplusplusnaturally.sf.More notes available at: ‡ http://www.htm ‡ http://www.blogspot.googlepages.com ‡ http://zainvi.computer-sciencenotes.co m/index.blogspot.com .

Sign up to vote on this title
UsefulNot useful