Class7

24
Const and the Copy Constructors

description

 

Transcript of Class7

Page 1: Class7

Const and the Copy Constructors

Page 2: Class7

Object Oriented Paradigm

• Separating interface from implementation• Constructors & destructors• Public vs private

• Not C++ specific

Page 3: Class7

Const – specific to C++

• const keyword• Ensures initialized value is its only value ever• Sometimes it’s a promise

Page 4: Class7

Example

Page 5: Class7

No call to Read/Write?

• No matching call to write(Point)?• We have function write(Point)!

• But wait…• Errors are on lines that contain P.reflectX()• Compiler sees write(Point&) as candidate, but

just won’t let you pass the result of P.refelctX() as a reference

Page 6: Class7

Read Problem

• read() tries to change what is returned by P.reflectX()

• P.reflectX() is rvalue• rvalues can only by on the right side of

assignment• (opposite are lvalues)

Page 7: Class7

But why an rvalue?

• Result of function call or expression evaluation is a temporary • Object exists temporarily until expression in

which it occurs is evaluated, then dies

• C++ does not let you modify temporaries• Does not let you use them as lvalues

Page 8: Class7

Our Errors

• Read() tries to modify result of function call, a temporary - Error

• Write doesn't try to modify temporary returned by the function call P.reflectX()• Why is this a problem then?

Page 9: Class7

Compiler Doesn’t Know

• Doesn’t know function won’t modify temporary

• So tell it…using const• Promise to compiler function won’t change

temporary

Page 10: Class7

Quick Fix

• Const fixed everything

Page 11: Class7

Why is write() a friend?

• Don’t need write() as friend• Can use public accessor functions

Page 12: Class7

What is THIS error?

In function `void write(const Point &)':

passing `const Point' as `this' argument of `double Point::getX()' discards qualifiers

passing `const Point' as `this' argument of `double Point::getY()' discards qualifiers

• First off – “this” is a pointer to object whose member function was called• Called made to A.foo()

• Inside foo() this points to A

• Called made to B.foo()• Inside foo() this points to B

Page 13: Class7

Errors

• In write() we have variable P • Type const Point &

• Compiler complains calling P’s (a const Point &) getX() discards qualifiers• const is a qualifier

• Compiler has no guarantee getx() won’t modify P

In function `void write(const Point &)': passing `const Point' as `this' argument of `double Point::getX()' discards qualifiers passing `const Point' as `this' argument of `double Point::getY()' discards qualifiers

Page 14: Class7

Not Discarding Qualifiers

• We know getX() doesn’t modify P

• So let the compiler know…use const

• Syntax • Put const immediately after parenthesized

argument list to member function

double getX() const { return x; }

double getY() const { return y; }

Page 15: Class7

Object Creation

Page 16: Class7

How Many Objects Created?

• Quick answer -- 7Default constructor! Default constructor! (2,3) (-1,5) Copy constructor! Copy constructor! 2-arg constructor! Point dies! Point dies! Copy constructor! 2-arg constructor! Point dies! Point dies! (0,4) Point dies! Point dies! Point dies!

Page 17: Class7

How Many Objects Created?

• Quick answer -- 7

Page 18: Class7

Where It Happen?

• Points P & Q created with default constructors when defined

• Two calls to midpoint each create point they return with call to 2-argument constructor

• Where do 3 extra points come from? • All created with the copy constructor • From pass-by-value calls to midpoint & write

• All of this extra work can be avoided by using pass by reference (with const) instead!

Page 19: Class7

Using Const (for optimization)

Page 20: Class7

Output

Default constructor! Default constructor! (2,3) (-1,5) 2-arg constructor! 2-arg constructor! (0,4) Point dies! Point dies! Point dies! Point dies!

Page 21: Class7

Things Get Messy

how many components? 3 Enter 3 values: 9.3 8.9 3.2 21.4 = 2.8771e-309 + 8.9 + 3.2

Page 22: Class7

Why?

• Sum makes copy of V calls it A• When V.val is copied to A.val

A.val points to same array

• When sum() finished A’s

destructor is called• Deletes A.val• But that’s V.val too!

Page 23: Class7

Two solutions

• Avoid pass-by-value, use pass-by-reference instead

• Define copy constructor that makes deep copy, including allocating new arrays

Page 24: Class7

Make it Go Away!

• You can make it all go away by using pass-by-reference in conjunction with const• Java, for example, doesn’t even have pass-

by-value for user defined types