CS 225 Lecture Notes - Lecture 9: Memory Management, Object-Oriented Programming
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.