Class7

639 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
639
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Class7

  1. 1. Const and the Copy Constructors
  2. 2. Object Oriented Paradigm <ul><li>Separating interface from implementation </li></ul><ul><ul><li>Constructors & destructors </li></ul></ul><ul><ul><li>Public vs private </li></ul></ul><ul><li>Not C++ specific </li></ul>
  3. 3. Const – specific to C++ <ul><li>const keyword </li></ul><ul><ul><li>Ensures initialized value is its only value ever </li></ul></ul><ul><ul><li>Sometimes it’s a promise </li></ul></ul>
  4. 4. Example
  5. 5. No call to Read/Write? <ul><li>No matching call to write(Point)? </li></ul><ul><ul><li>We have function write(Point)! </li></ul></ul><ul><li>But wait… </li></ul><ul><ul><li>Errors are on lines that contain P.reflectX() </li></ul></ul><ul><ul><li>Compiler sees write(Point&) as candidate, but just won’t let you pass the result of P.refelctX() as a reference </li></ul></ul>
  6. 6. Read Problem <ul><li>read() tries to change what is returned by P.reflectX() </li></ul><ul><li>P.reflectX() is rvalue </li></ul><ul><ul><li>rvalues can only by on the right side of assignment </li></ul></ul><ul><ul><li>(opposite are lvalues) </li></ul></ul>
  7. 7. But why an rvalue? <ul><li>Result of function call or expression evaluation is a temporary </li></ul><ul><ul><li>Object exists temporarily until expression in which it occurs is evaluated, then dies </li></ul></ul><ul><li>C++ does not let you modify temporaries </li></ul><ul><ul><li>Does not let you use them as lvalues </li></ul></ul>
  8. 8. Our Errors <ul><li>Read() tries to modify result of function call, a temporary - Error </li></ul><ul><li>Write doesn't try to modify temporary returned by the function call P.reflectX() </li></ul><ul><ul><li>Why is this a problem then? </li></ul></ul>
  9. 9. Compiler Doesn’t Know <ul><li>Doesn’t know function won’t modify temporary </li></ul><ul><li>So tell it…using const </li></ul><ul><ul><li>Promise to compiler function won’t change temporary </li></ul></ul>
  10. 10. Quick Fix <ul><li>Const fixed everything </li></ul>
  11. 11. Why is write() a friend? <ul><li>Don’t need write() as friend </li></ul><ul><ul><li>Can use public accessor functions </li></ul></ul>
  12. 12. What is THIS error? <ul><li>In function `void write(const Point &)': </li></ul><ul><li>passing `const Point' as `this' argument of `double Point::getX()' discards qualifiers </li></ul><ul><li>passing `const Point' as `this' argument of `double Point::getY()' discards qualifiers </li></ul><ul><li>First off – “this” is a pointer to object whose member function was called </li></ul><ul><ul><li>Called made to A.foo() </li></ul></ul><ul><ul><ul><li>Inside foo() this points to A </li></ul></ul></ul><ul><ul><li>Called made to B.foo() </li></ul></ul><ul><ul><ul><li>Inside foo() this points to B </li></ul></ul></ul>
  13. 13. Errors <ul><li>In write() we have variable P </li></ul><ul><ul><li>Type const Point & </li></ul></ul><ul><li>Compiler complains calling P’s (a const Point & ) getX() discards qualifiers </li></ul><ul><ul><li>const is a qualifier </li></ul></ul><ul><li>Compiler has no guarantee getx() won’t modify P </li></ul>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
  14. 14. Not Discarding Qualifiers <ul><li>We know getX() doesn’t modify P </li></ul><ul><li>So let the compiler know…use const </li></ul><ul><li>Syntax </li></ul><ul><ul><li>Put const immediately after parenthesized argument list to member function </li></ul></ul><ul><ul><li>double getX() const { return x; } </li></ul></ul><ul><ul><li>double getY() const { return y; } </li></ul></ul>
  15. 15. Object Creation
  16. 16. How Many Objects Created? <ul><li>Quick answer -- 7 </li></ul>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. 17. How Many Objects Created? <ul><li>Quick answer -- 7 </li></ul>
  18. 18. Where It Happen? <ul><li>Points P & Q created with default </li></ul><ul><li>constructors when defined </li></ul><ul><li>Two calls to midpoint each create </li></ul><ul><li>point they return with call to </li></ul><ul><li>2-argument constructor </li></ul><ul><li>Where do 3 extra points come from? </li></ul><ul><ul><li>All created with the copy constructor </li></ul></ul><ul><ul><li>From pass-by-value calls to midpoint & write </li></ul></ul><ul><ul><li>All of this extra work can be avoided by using pass by reference (with const) instead! </li></ul></ul>
  19. 19. Using Const (for optimization)
  20. 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. 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. 22. Why? <ul><li>Sum makes copy of V calls it A </li></ul><ul><ul><li>When V.val is copied to A.val </li></ul></ul><ul><ul><li>A.val points to same array </li></ul></ul><ul><li>When sum() finished A’s </li></ul><ul><li>destructor is called </li></ul><ul><ul><li>Deletes A.val </li></ul></ul><ul><ul><li>But that’s V.val too! </li></ul></ul>
  23. 23. Two solutions <ul><li>Avoid pass-by-value, use pass-by-reference instead </li></ul><ul><li>Define copy constructor that makes deep copy , including allocating new arrays </li></ul>
  24. 24. Make it Go Away! <ul><li>You can make it all go away by using pass-by-reference in conjunction with const </li></ul><ul><ul><li>Java, for example, doesn’t even have pass-by-value for user defined types </li></ul></ul>

×