CS 225 Lecture Notes - Lecture 9: Memory Management, Object-Oriented Programming

29 views4 pages

Document Summary

Mp2 available, due 9/13, 11:59p b=a operator =, where does it live if: sphere b, a; // it lives in the sphere class. We need a smart copy if we use the assignment operator on two objects independent copy. We need to check whether the objects are already the same so prevent self assignment c. op=(b. op=(a)); // this is equivalent to c=b=a (b. op=(a)) must return a sphere. // every assignment operator should be in this order. // if const was in the beggining, returning a const sphere is fine too sphere & sphere::operator=(const sphere & rhs) // (sphere const & rhs) is the same param. // protect against re-assignment if(this != &rhs) return; // copy(); radius = rhs. radius; numatts = rhs. numatts; atts = new string[numatts]; Cs 225 - lecture page 1 for(int i = 0; i < numatts; i++) atts[i] = rhs. atts[i]; // return a helpful value return *this; // type is sphere. // the helper fucntions should be in private.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents