Title: Const and the Copy Constructors
1Const and the Copy Constructors
2Object Oriented Paradigm
- Separating interface from implementation
- Constructors destructors
- Public vs private
- Not C specific
3Const specific to C
- const keyword
- Ensures initialized value is its only value ever
- Sometimes its a promise
4Example
5No 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 wont let you pass the result of
P.refelctX() as a reference
6Read 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)
7But 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
8Our 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?
9Compiler Doesnt Know
- Doesnt know function wont modify temporary
- So tell itusing const
- Promise to compiler function wont change
temporary
10Quick Fix
11Why is write() a friend?
- Dont need write() as friend
- Can use public accessor functions
12What is THIS error?
- In function void write(const Point )'
- passing const Point' as this' argument of
double PointgetX()' discards qualifiers - passing const Point' as this' argument of
double PointgetY()' 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
13Errors
In function void write(const Point )' passing
const Point' as this' argument of double
PointgetX()' discards qualifiers passing
const Point' as this' argument of double
PointgetY()' discards qualifiers
- In write() we have variable P
- Type const Point
- Compiler complains calling Ps (a const Point )
getX() discards qualifiers - const is a qualifier
- Compiler has no guarantee getx() wont modify P
14Not Discarding Qualifiers
- We know getX() doesnt modify P
- So let the compiler knowuse const
- Syntax
- Put const immediately after parenthesized
argument list to member function - double getX() const return x
- double getY() const return y
15Object Creation
16How Many Objects Created?
Default 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!
17How Many Objects Created?
18Where 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!
19Using Const (for optimization)
20Output
Default constructor! Default constructor! (2,3)
(-1,5) 2-arg constructor! 2-arg constructor!
(0,4) Point dies! Point dies! Point dies!
Point dies!
21Things Get Messy
how many components? 3 Enter 3 values 9.3 8.9
3.2 21.4 2.8771e-309 8.9 3.2
22Why?
- 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 As
- destructor is called
- Deletes A.val
- But thats V.val too!
23Two solutions
- Avoid pass-by-value, use pass-by-reference
instead - Define copy constructor that makes deep copy,
including allocating new arrays
24Make it Go Away!
- You can make it all go away by using
pass-by-reference in conjunction with const - Java, for example, doesnt even have
pass-by-value for user defined types