Const and the Copy Constructors - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Const and the Copy Constructors

Description:

Compiler complains calling P's (a const Point &) getX() discards qualifiers ... Not Discarding Qualifiers. We know getX() doesn't modify P. So let the compiler ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 25
Provided by: bal1
Category:

less

Transcript and Presenter's Notes

Title: Const and the Copy Constructors


1
Const and the Copy Constructors
2
Object Oriented Paradigm
  • Separating interface from implementation
  • Constructors destructors
  • Public vs private
  • Not C specific

3
Const specific to C
  • const keyword
  • Ensures initialized value is its only value ever
  • Sometimes its a promise

4
Example
5
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 wont let you pass the result of
    P.refelctX() as a reference

6
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)

7
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

8
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?

9
Compiler Doesnt Know
  • Doesnt know function wont modify temporary
  • So tell itusing const
  • Promise to compiler function wont change
    temporary

10
Quick Fix
  • Const fixed everything

11
Why is write() a friend?
  • Dont need write() as friend
  • Can use public accessor functions

12
What 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

13
Errors
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

14
Not 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

15
Object Creation
16
How Many Objects Created?
  • Quick answer -- 7

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!
17
How Many Objects Created?
  • Quick answer -- 7

18
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!

19
Using Const (for optimization)
20
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!
21
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
22
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 As
  • destructor is called
  • Deletes A.val
  • But thats V.val too!

23
Two solutions
  • Avoid pass-by-value, use pass-by-reference
    instead
  • Define copy constructor that makes deep copy,
    including allocating new arrays

24
Make 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
Write a Comment
User Comments (0)
About PowerShow.com