Programming with ANSI C ++ A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
and User Defined Conversions Chapter 6 Operator Overloading and User Defined Conversions
Introduction Give an additional meaning to an existing operator A Meaningful meaning! Is important The operator functions
Operator Overloading and User Defined Conversions Comparison of both the methods of conversion Which method is to be used? Conversion and OO should not be changing the semantics of use The stack example for + and – The employee example for + and -
Introduction Give an additional meaning to an existing operator A Meaningful meaning! Is important The operator functions
Restrictions on use of OO Cannot change the original meaning New operators cannot be devised Ops will not have additional precedence. cannot change number of arguments at least one argument as user defined type Except (), no other operator can have a default argument. same return type for new, delete and ->
Operators which can not be overloaded The dot operator for member access The dereference member to class operator .* Scope resolution operator :: Size of operator sizeof Conditional ternary operator ?: Casting operators static_cast<>, dynamic_cast<>, reinterpret_cast<>, const_cast<> # and ## tokens for macro preprocessors
Operators which can not be overloaded as friends Assignment Operator = Function call operator () Array Subscript operator [] Access to class member using pointer to object operator -> To ensure their first operand is lvalue! = has global meaning as well!
The operator function The need for word operator preceding the real operator like operator + No explicit call! Better as a library function! Users do not need to remember function names No ambiguity about parameters and call Readability increases No misspelled functions
Types of operators Unary Operators Binary Operators The dummy arguments for postfix versions of increment and decrement operators Binary Operators Overloading shorthand operators while overloading arithmetic operators The single ternery operator is not available
Using Friends The lvalue problem for cases like Object + scaler and Scaler + Object calls to + The body for both the cases can be different! The operator << and >> case The need to return reference
Overloading other Operators The = operator Normal Overloading Extending = in the derived class The [] operator The safearray class example The need to have reference as return type Can be extended using templates
Overloading other Operators The () operator The function object vs. Function pointers Inlining is possible May have cache info and help The new and delete using malloc and free The New and delete using ::new and ::delete
Overloading new and delete Before Insertion After First Insertion Old Pointer Table Space pointer Current Pointer Null After 8th Insertion Overloading new and delete
User Defined Conversions Conversion from built-in data type to an object Object to a built-in data type The need for such operator: the wrapper class example Object to Object Conversion using constructors using conversion functions
Operator Overloading and User Defined Conversions Comparison of both the methods of conversion Which method is to be used? Conversion and OO should not be changing the semantics of use The stack example for + and – The employee example for + and -