COMP 206 Unit 11: Operator Overloading

Description

University-level C++ COMP 206 Flashcards on COMP 206 Unit 11: Operator Overloading, created by Adriana Vincelli-Joma on 13/10/2021.
Adriana Vincelli-Joma
Flashcards by Adriana Vincelli-Joma, updated more than 1 year ago
Adriana Vincelli-Joma
Created by Adriana Vincelli-Joma about 3 years ago
5
0

Resource summary

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
Show full summary Hide full summary

Similar

COMP 206 Unit 5: Hiding the Implementation
Adriana Vincelli-Joma
COMP 206 Unit 6: Initialization and Cleanup
Adriana Vincelli-Joma
COMP 206 Unit 7: Function Overloading and Default Arguments
Adriana Vincelli-Joma
COMP 206 Unit 8: Constants
Adriana Vincelli-Joma
COMP 206 Unit 9: Name Control
Adriana Vincelli-Joma
COMP 206 Unit 12: Dynamic Object Creation
Adriana Vincelli-Joma
COMP 206 Unit 13: Inheritance and Composition
Adriana Vincelli-Joma
COMP 206 Unit 3: The C in C++
Adriana Vincelli-Joma
GOVN 377 Unit 8: The Brave New World of Data Management and Manipulation
Adriana Vincelli-Joma
GOVN 377 Unit 6: New Technology: Data Mining, Matching, and Management
Adriana Vincelli-Joma
GOVN 377 Unit 7: Research and the Implications for Information Access adn Privacy
Adriana Vincelli-Joma