![Page 1: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/1.jpg)
QUIZ
Explain the meaning of each variable declared:
• Data d, *dp = &d;
• int Data::*ptr=&Data::a;
• int (Data::*fp2) (float);
• int (Foo::*fptr) (string);
• int (Foo::*fptr) (string) = &Foo::f;
![Page 2: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/2.jpg)
QUIZ
Explain from context the meaning of foo, bar, baz and qux in the following code:
• (foo.*bar)(42, 43);
• float f = (baz->*qux)();
• foo.*bar = 42;
• baz->*qux = 43.5;
![Page 3: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/3.jpg)
QUIZ The following class exists in a program:
class A{
public:
int m;
int n;
};
Declare and initialize a pointer to each of its members.
![Page 4: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/4.jpg)
QUIZ The following class exists in a program:
class A{
public:
int m;
int n;
};
int A::*pm_m = &A::m;
int A::*pm_n = &A::n;
![Page 5: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/5.jpg)
QUIZ
Instantiate two objects of class A, x and y. Use the pointers to set the following values for their members: • x: 1, 2 • y: 3, 4
class A{
public:
int m;
int n;
};
int A::*pm_m = &A::m;
int A::*pm_n = &A::n;
![Page 6: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/6.jpg)
QUIZ class A{
public:
int m;
int n;
};
int A::*pm_m = &A::m;
int A::*pm_n = &A::n;
A x, y; x.*pm_m = 1; x.*pm_n = 2; y.*pm_m = 3; y.*pm_n = 4;
![Page 7: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/7.jpg)
QUIZ class A{
public:
int m;
int n;
};
Declare a public member function add() that returns the sum of the two data members.
Declare and initialize a pointer to add().
A x;
![Page 8: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/8.jpg)
QUIZ class A{
public:
int m;
int n;
int add(){return m+n;}
};
Use the pointer to display the sum of the two members of x.
A x; int (A::*fp)(); fp = &A::add;
![Page 9: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/9.jpg)
QUIZ class A{
public:
int m;
int n;
int add(){return m+n;}
};
A x; int (A::*fp)(); fp = &A::add; cout <<(x.*fp)();
![Page 10: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/10.jpg)
Ch. 12: Operator Overloading
![Page 11: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/11.jpg)
Operator overloading is just “syntactic sugar,” i.e. another way to make a function call:
shift_left(42, 3); 42 << 3; The difference is that the arguments for this function don’t appear inside parentheses, but instead they surround or are next to the operator’s character(s).
example
![Page 12: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/12.jpg)
Let’s first understand what we’re trying to do!
Just a “wrapper” for the int type!
![Page 13: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/13.jpg)
![Page 14: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/14.jpg)
Wait a second! Isn’t + a binary operator?
![Page 15: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/15.jpg)
Yes, but, when defined as member function, the LEFT operand is always the object, so only the RIGHT operand needs to be passed.
![Page 16: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/16.jpg)
Why is the return value const?
![Page 17: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/17.jpg)
In order to prevent “crazy” uses like (x + y) = z;
![Page 18: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/18.jpg)
Write the member function to overload the division / operator.
QUIZ
![Page 19: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/19.jpg)
Must #include <cassert>
Note: The author of the text has his own, customized version of assert, called require:
…..
![Page 20: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/20.jpg)
Now let’s overload the compound assignment operator +=
![Page 21: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/21.jpg)
. . .
Why non-const and why reference?
![Page 22: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/22.jpg)
. . .
Non-const to prevent uses like (x += y) += z;
Reference to prevent the creation of a copy.
![Page 23: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/23.jpg)
. . .
If this is a pointer to the current object, *this is …
![Page 24: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/24.jpg)
![Page 25: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/25.jpg)
Overloadable operators Although you can overload almost all the operators available in C, the use of operator overloading is fairly restrictive:
• cannot combine operators that currently have no meaning in C (such as ** to represent exponentiation)
• cannot change the evaluation precedence of operators
• cannot change the number of arguments required by an operator.
![Page 26: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/26.jpg)
Unary operators
. . .
declarations
![Page 27: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/27.jpg)
. . .
definitions
![Page 28: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/28.jpg)
Examples with member functions
. . .
![Page 29: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/29.jpg)
Thoroughly read and understand the entire subsection Unary operators, including Increment and Decrement.
We stop before the subsection
Binary operators
![Page 30: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/30.jpg)
Individual work for next time:
End-of-chapter exercises 1, 2, 3
EOL 30
![Page 31: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/31.jpg)
Based on their “signatures” (i.e. function headers), explain what the compiler does when it encounters each operator.
QUIZ
What is this operator called?
![Page 32: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/32.jpg)
When the compiler sees ++a (a pre-increment), it generates a call to operator++(a); but when it sees a++, it generates a call to operator++(a, int). That is, it differentiates between the two forms by making calls to different overloaded functions.
QUIZ
![Page 33: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/33.jpg)
Why does Integer have to “befriend” all these functions?
QUIZ
. . . .
![Page 34: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/34.jpg)
B/c they’re global functions, and the member is private.
QUIZ
. . . .
![Page 35: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/35.jpg)
Binary operators Assignment can only be implemented as member function (not global!), that’s why we have it only in the Byte class:
?
More in the separate section Overloading assignment
![Page 36: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/36.jpg)
Binary operators
More of the same …
Read this section!
![Page 37: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/37.jpg)
1.If you only need to read from the argument and not change it, default to passing it as a const reference.
• Ordinary arithmetic operations (like + and –, etc.) and Booleans will not change their arguments, so pass by const reference is predominantly what you’ll use.
• When the function is a class member, this translates to making it a const member function.
Arguments & return values - GUIDELINES -
![Page 38: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/38.jpg)
1.If you only need to read from the argument and not change it, default to passing it as a const reference.
• Only with the operator-assignments (like +=) and the operator=, which change the left-hand argument, is the left argument not a constant, but it’s still passed in as an address because it will be changed.
Arguments & return values - GUIDELINES -
![Page 39: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/39.jpg)
2. The type of return value you should select depends on the expected meaning of the operator.
• If the effect of the operator is to produce a new value, you will need to generate a new object as the return value. For example, Integer::operator+ must produce an Integer object that is the sum of the operands. This object is returned by value as a const, so the result cannot be modified as an lvalue.
Arguments & return values - GUIDELINES -
![Page 40: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/40.jpg)
Arguments & return values
3. All the assignment operators modify the lvalue. To allow the result of the assignment to be used in chained expressions, like a=b=c, it’s expected that you will return a reference to that same lvalue that was just modified.
• But should this reference be a const or nonconst? Although you read a=b=c from left to right, the compiler parses it from right to left, so you’re not forced to return a nonconst to support assignment chaining.
- GUIDELINES -
Assignment is right-associative
![Page 41: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/41.jpg)
What is the output?
QUIZ
![Page 42: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/42.jpg)
It’s the same as if parenthesized
thus:
QUIZ
![Page 43: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/43.jpg)
3. All the assignment operators modify the lvalue. To allow the result of the assignment to be used in chained expressions, like a=b=c, it’s expected that you will return a reference to that same lvalue that was just modified.
• However, people do sometimes expect to be able to perform an operation on the thing that was just assigned to, such as (a=b).func( ); to call func( ) on a after assigning b to it. Thus, the return value for all of the assignment operators should be a nonconst reference to the lvalue.
Arguments & return values - GUIDELINES -
![Page 44: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/44.jpg)
vs.
Returning a temporary a.k.a. the return value optimization
The compiler builds the object directly into the location of the outside return value. Only constructor is called. No copy-constructor is called! No destructor is called (no object in this function’s scope – leave it to the caller’s scope!)
Constructor, copy-constructor, and destructor are called.
![Page 45: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/45.jpg)
Read over lightly:
operator[]
new
delete
operator,
![Page 46: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/46.jpg)
Operator->
Generally used when you want to make an object appear to be a pointer.
Since such an object has more “smarts” built into it than exist for a typical pointer, an object like this is often called a smart pointer.
![Page 47: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/47.jpg)
Operator->
Especially useful if you want to:
• “wrap” a class around a pointer to make it safe
• create an iterator (object that moves through a collection /container of other objects and selects them one at a time, without providing direct access to the implementation of the container)
![Page 48: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/48.jpg)
Operator->
A pointer dereference operator must:
• be a member function
• return an object (or reference to an object) that also has a pointer dereference operator, or
• return a pointer that can be used to select what the pointer dereference operator arrow is pointing at.
example
![Page 49: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/49.jpg)
![Page 50: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/50.jpg)
![Page 51: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/51.jpg)
It’s more common to see a “smart pointer” or “iterator” class nested within
the class that it services – see the next example NestedSmartPointer.cpp
![Page 52: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/52.jpg)
. . . . . Member function of
the vector class
nullptr
![Page 53: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/53.jpg)
![Page 54: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/54.jpg)
Abort at the end of container
Let’s understand this! sp is an object of class SmartPointer …
(next slide)
![Page 55: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/55.jpg)
![Page 56: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/56.jpg)
Shouldn’t it be sp -> -> f() instead?
![Page 57: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/57.jpg)
Shouldn’t it be sp -> -> f() instead?
A: Yes, technically it should, but the compiler is built to automatically do the work of the second -> operator.
![Page 58: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/58.jpg)
We stop before the subsection
Operators you can’t overload
EOL 31
![Page 59: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/59.jpg)
Operators you can’t overload
• The member selection operator.
• The pointer to member dereference operator.*
• There’s no exponentiation operator. The most popular choice for this was operator** from Fortran, but this raised difficult parsing questions. Also, C has no exponentiation operator, so C++ didn’t seem to need one either because you can always perform a function call.
• There are no user-defined operators.
• You can’t change the precedence rules.
![Page 60: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/60.jpg)
Non-member operators
If it doesn’t make any difference whether we overload the operators with member or global functions, it is recommended to choose members; this emphasizes the association between the operator and its class.
![Page 61: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/61.jpg)
Non-member operators
However, sometimes you want the left-hand operand to be an object of some other class.
A common application is when the operators << and >> are overloaded for iostreams; we want to be able to write:
MyClass myObject;
cout <<myObject;
cin >>myObject;
![Page 62: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/62.jpg)
![Page 63: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/63.jpg)
These are global functions, not members!
![Page 64: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/64.jpg)
Remember stringstream from Lab 7!
![Page 65: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/65.jpg)
Overloading assignment
In which of these commands is the copy-constructor called?
Hint: (Only) when a new object is created from an existing object!
![Page 66: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/66.jpg)
The right-hand side does not even need to be a user-defined object:
Assignment syntax
Constructor syntax (recommened!)
![Page 67: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/67.jpg)
Conclusion: Any time you’re initializing an object using an = instead of the ordinary function-call form of the constructor, the compiler will look for a constructor that accepts whatever is on the right-hand side.
![Page 68: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/68.jpg)
![Page 69: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/69.jpg)
One simple rule: copy all of the necessary information from the right-hand object into the current object (that is, the object that operator= is being called for)
![Page 70: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/70.jpg)
Common mistake: you should always check first for self-assignment!
In some cases, such as this one, it’s harmless if you perform the assignment operations anyway, but if changes are made to the implementation of the class, it can make a difference, and if you don’t do it as a matter of habit, you may forget and cause hard-to-find bugs.
![Page 71: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/71.jpg)
Pointers in classes
Problem: Simply copying a pointer means that you’ll end up with two objects pointing to the same storage location.
Solution: You need to do bookkeeping of your own.
example
![Page 72: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/72.jpg)
![Page 73: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/73.jpg)
![Page 74: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/74.jpg)
Simply copy whatever the pointer refers to. (Also works for copy-
construction.)
![Page 75: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/75.jpg)
Problem with the copy technique
If the object requires a lot of memory or time for initialization, copying is not efficient.
Solution: next slide
![Page 76: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/76.jpg)
Reference counting
You give intelligence to the object that’s being pointed to so it knows how many objects are pointing to it.
Both copy-construction and assignment mean: attaching another pointer to an existing object and incrementing the reference count.
Destruction means: decrementing the reference count. If the reference count goes to zero, destroy the object!
![Page 77: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/77.jpg)
The program
C12:ReferenceCounting.cpp
is not required for the exam.
Read FYI.
![Page 78: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/78.jpg)
Automatic operator= creation
Because assigning an object to another object of the same type is an activity most people expect to be possible, the compiler will automatically create a type::operator=(type) if you don’t make one.
The behavior of this operator mimics that of the automatically created copy-constructor; if the class contains objects (or is inherited from another class), the operator= for those objects is called recursively, a.k.a. memberwise assignment.
![Page 79: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/79.jpg)
Remember from ch.11: “Deleted” functions were introduced in
C++11
![Page 80: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/80.jpg)
![Page 81: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/81.jpg)
We stop before the section
Automatic type conversion
SKIP the remainder of ch.12
![Page 82: Welcome to Introduction to Object-Oriented Programming (OOP) · vs. Returning a temporary a.k.a. the return value optimization The compiler builds the object directly into the location](https://reader034.vdocuments.site/reader034/viewer/2022052105/604046815eaeba0d3869abba/html5/thumbnails/82.jpg)
Homework for ch. 12
Provided as separate handout (also available on our webpage --> agapie.net)
Due Friday, Nov. 20, at the beginning of class.
Please hand in a hard-copy, do not email!
EOL 32