13.07.2015 Views

BABAR C++ Course Running the Examples - HEPHY

BABAR C++ Course Running the Examples - HEPHY

BABAR C++ Course Running the Examples - HEPHY

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

operator= Member FunctionAssignment versus CopyImplementationSimpleFloatArray&SimpleFloatArray::operator=(const SimpleFloatArray& rhs)if ( ptr_to_data != rhs.ptr_to_data ) {setSize( rhs.num_elts );copy(rhs);}return *this;}• if() statements tests that array object is not beingassigned to itself.• this is a pointer to <strong>the</strong> object with which <strong>the</strong>member function was called.• this->ptr_to_data and ptr_to_data areequivalent• must implement else default is member-wise copyleading to two objects sharing <strong>the</strong> same dataCopy ConstructorSimpleFloatArray::SimpleFloatArray(const SimpleFloatArray& a) {num_elts = a.num_elts;ptr_to_data = new float[num_elts];copy(a); // Copy a's elements}Assignment operatorSimpleFloatArray&SimpleFloatArray::operator=(const SimpleFloatArray& rhs) {if ( ptr_to_data != rhs.ptr_to_data ) {setSize( rhs.num_elts );copy(rhs);}return *this;}UseSimpleFloatArray x(n);SimpleFloatArray y = x; // copy constructorSimpleFloatArray z;//z = x // copy array // assignment• should not implement one without <strong>the</strong> o<strong>the</strong>r<strong>BABAR</strong> <strong>C++</strong> <strong>Course</strong> 107 Paul F. Kunz<strong>BABAR</strong> <strong>C++</strong> <strong>Course</strong> 108 Paul F. Kunz

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!