Flashcards
COMP 206 Unit 11: Operator Overloading
Cards in this deck (22)
- 1Dangers of Operator Overloading- no reason to overload except to make code easier to write/read
- built-in data types cannot be changed - 2Syntax for Operator Overloading- operator@
- @: operator being overloaded (e.g. >>, +, -, etc.) - 3Two Factors that Govern Number of Arguments in List1. whether unary operator or binary operator
2. whether operator is defined as global function or member function - 4Unary Operators that can be Overloaded+, - ,~, &, ++, --, !
- 5Binary Operators that can be Overloaded+, -, *, /, %, ^, &, |, <<, >>, +=, -=, *=, /=, %=, ^=, &=, |=, ==, >>=, !=, <=, <<=, >=, >, <, &&, ||,
- 64 Steps in Pattern that Govern Arguments and Return Values1. read from argument and not change it, default to it as const reference
2. type of return value you should select depends on expected meaning of operator
3. all assignment operators modify lvalue; return value for all assignment operators should be nonconst
4. for logical operators, get back int or bool - 7Unusual Operators that can be Overloaded, : not called for function arg. list, only for objects that are not in open, separated by commas
-> : generally used when you want to make object appear to be pointer
->* : provided for situations when you want to mimic behavior provided by built-in pointer-to-member syntax - 8Operators that cannot be overloaded..
.*,
**
user-defined operator
precedence rules - 9Member vs Non-member Operators- operators should be members to emphasize association between operator and class
- non-member operator overloading function simply has right name and does what you want - 10Guidelines for Operator Overloading- member: all unary operators
- must be member: =, (), [], ->, ->*
- member: +=, -=, /=, *=, ^=, &=, |=, %=, >>=. <<=
- non-member: all other binary operators - 11Creating Overloaded Assignment Operator- must be overloaded as member function
- operator= takes as argument whatever appears on RHS
- must copy all necessary info. from RHS object into current object to perform assignment
- object on LHS of = copies all elements of object on right, then returns reference to itself - 12Behavior of operator= with respect to pointers in classes- operator= checks for self-assignment
- copy-constructor and operator= make new copy of what pointer points to, destructor deletes it - 13reference countinggives intelligence to object that's being pointed to so it knows how many objects are pointing to it
- 14copy-on-writemakes sure no one else is using block of memory
- 15Operator= with respect to reference counting- operator= deals with object that has already been created on left-side of =
- operator= first checks to detect whether you're assigning same object to itself - 16automatic operator= creationwhen assigning object to another object of same type, compiler will automatically create type::operator= (type) if you don't make one
- 17automatic type conversionused to convert type of expression/function call has to type compiler wants
- 18Automatic Type Conversion in Constructorsif you define constructor that takes as its single argument an object/reference of another type, that constructor allows compiler to perform automatic type conversion
- 19Prevent automatic type conversion in constructors- turn off automatic type conversion; modify constructor by prefacing with keyword explicit
- if user wants to make conversion happen, code must be written out - 20Automatic type conversion in conjunction with operator overloading- create member function that takes current type and converts it to desired type using operator keyword followed by type you want to convert to
- return type is name of operator you're overloading - 21reflexivityitem related to itself
- 22Pitfalls of automatic type conversion- class X can convert itself to object of class Y with operator Y(); generates ambiguity error
- provide automatic conversion to more than one type
Information
- Description
- University-level C++ COMP 206 Flashcards on COMP 206 Unit 11: Operator Overloading, created by Adriana Vincelli-Joma on 13/10/2021.
- Created by
- Adriana Vincelli-Joma
- Views
- 11
- Tags
- Rating