Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Overloading operators
Comp Sci 1570 Introduction to C++
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Overloading operators
• Function overloading provides a mechanism to create andresolve function calls to multiple functions with the samename, so long as each function has a unique functionprototype.
• This allows you to create variations of a function to workwith different data types, without having to think up aunique name for each variant.
• In C++, operators are implemented as functions.• By using function overloading on the operator functions,
you can define your own versions of the operators thatwork with different data types (including classes thatyou’ve written).
• Using function overloading to overload operators is calledoperator overloading.
ReturnType c l a s s n a m e ( o p t i o n a l ) : : ope ra to r OperatorSymbol ( argument l i s t ){
\\ s t a t e m e n t s ;}
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Rules for overloading operators
• You cannot create new operators.
• If you overload an operator, at least one of the parametersmust be a user-defined type. Thus, you cannot redefine anoperator for a built-in type.
• There are some operators you are prevented fromoverloading (see tables upcoming)
• You cannot change the arity (number of operandsrequired) of a operator.
• You cannot change the order of precedence or associativityof operators by overloading them.
• An overloaded operator cannot have default arguments.
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Operators can be overloaded in several locations
• As a member function
• As a normal non-member function
• As a friend non-member function
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Operator+
• See code implementing it all 3 ways
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Overloading as a member function
• For example, the assignment (=), subscript ([]), functioncall (()), and member selection (− >) operators must beoverloaded as member functions
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Overload as non-member and Non-member friend
• For example, we are not able to overload operator << ()as a member function.
• The overloaded operator must be added as a member ofthe left operand.
• In this case, the left operand is an object of typestd::ostream. std::ostream is fixed as part of the standardlibrary. We don’t modify the class declaration to add theoverload as a member function of std::ostream.
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
When to use normal, friend, or member overload
• When dealing with binary operators that don’t modify theleft operand (e.g. operator+), the normal or friendfunction version is typically preferred, because it works forall parameter types (even when the left operand isn’t aclass object, or is a class that is not modifiable). Thenormal or friend function version has the added benefit of“symmetry”, as all operands become explicit parameters(instead of the left operand becoming *this and the rightoperand becoming an explicit parameter).
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
When to use normal, friend, or member overload
• When dealing with binary operators that do modify theleft operand (e.g. operator+=), the member functionversion is typically preferred. In these cases, the leftmostoperand will always be a class type, and having the objectbeing modified become the one pointed to by *this isnatural. Because the rightmost operand becomes anexplicit parameter, there’s no confusion over who isgetting modified and who is getting evaluated.
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
When to use normal, friend, or member overload
• Unary operators are usually overloaded as memberfunctions as well, since the member version has noparameters.
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Summary for which method
• If you’re overloading assignment (=), subscript ([]),function call (()), or member selection (− >), do so as amember function.
• If you’re overloading a unary operator, do so as a memberfunction.
• If you’re overloading a binary operator that modifies itsleft operand (e.g. operator+=), do so as a memberfunction if you can.
• If you’re overloading a binary operator that does notmodify its left operand (e.g. operator+), do so as anormal function or friend function.
• Remember: Which method and syntax you use for eachoperator depends on the particular arbitraryimplementation in C++, and requireschecking/memorizing the rules
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Arithmetic operators
• Here, a, b, and c represent valid values (literals, valuesfrom variables, or return value), object names, or lvalues,as appropriate.
• R, S , and T , stand for any type(s), and K for a class typeor enumerated type.
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Comparison and relational operators
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Logical operators
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Bitwise operators
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Compound assignment operators
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Member and pointer operators
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Outline
1 Definitions
2 RulesBasics
3 Methods of operator overloadBasic examplesMemberNon-member and non-member friendWhen to use each
4 Overview of operatorsArithmeticComparison and relationalLogicalBitwiseCompound assignmentMember and pointerOther
Definitions
Rules
Basics
Methods ofoperatoroverload
Basic examples
Member
Non-memberand non-memberfriend
When to useeach
Overview ofoperators
Arithmetic
Comparison andrelational
Logical
Bitwise
Compoundassignment
Member andpointer
Other
Other operators