Created by Adriana Vincelli-Joma
about 3 years ago
|
||
Question | Answer |
Dangers of Operator Overloading | - no reason to overload except to make code easier to write/read - built-in data types cannot be changed |
Syntax for Operator Overloading | - operator@ - @: operator being overloaded (e.g. >>, +, -, etc.) |
Two Factors that Govern Number of Arguments in List | 1. whether unary operator or binary operator 2. whether operator is defined as global function or member function |
Unary Operators that can be Overloaded | +, - ,~, &, ++, --, ! |
Binary Operators that can be Overloaded | +, -, *, /, %, ^, &, |, <<, >>, +=, -=, *=, /=, %=, ^=, &=, |=, ==, >>=, !=, <=, <<=, >=, >, <, &&, ||, |
4 Steps in Pattern that Govern Arguments and Return Values | 1. 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 |
Unusual 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 |
Operators that cannot be overloaded | .. .*, ** user-defined operator precedence rules |
Member 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 |
Guidelines for Operator Overloading | - member: all unary operators - must be member: =, (), [], ->, ->* - member: +=, -=, /=, *=, ^=, &=, |=, %=, >>=. <<= - non-member: all other binary operators |
Creating 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 |
Behavior 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 |
reference counting | gives intelligence to object that's being pointed to so it knows how many objects are pointing to it |
copy-on-write | makes sure no one else is using block of memory |
Operator= 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 |
automatic operator= creation | when assigning object to another object of same type, compiler will automatically create type::operator= (type) if you don't make one |
automatic type conversion | used to convert type of expression/function call has to type compiler wants |
Automatic Type Conversion in Constructors | if you define constructor that takes as its single argument an object/reference of another type, that constructor allows compiler to perform automatic type conversion |
Prevent 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 |
Automatic 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 |
reflexivity | item related to itself |
Pitfalls 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 |
Want to create your own Flashcards for free with GoConqr? Learn more.