DOC PREVIEW
Duke CPS 108 - Playing well with Java Collections

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Playing well with Java CollectionsPlaying well with C++copy constructorAssignment operatorDestructorCPS 108 : Fall 20024.1Playing well with Java CollectionsEvery object has an equals(..) method, contract?What does this return? How do you implement it, what about apples and oranges?Default behavior? When to over-ride?If you override/over-ride equals, see hashCode()What does this return? Implementation issues?Good, bad, …?CPS 108 : Fall 20024.2Playing well with C++What happens with the statement myDay = d; ?assignment is memberwise unless operator = overloadedcopy constructor used in passing parameters by valueIf you need one of: destructor, assignment operator, copy constructor, you need all of themheuristic only: managing resources other than memorypreventing objects from being copiedwhat about non-copyable state, e.g., streamIn assignment operator, watch for self-assignmentStudy implementation of string/vectorCPS 108 : Fall 20024.3copy constructorUsed for “first-time” creationDate d(1,1,2000);Date copy(d); Used for pass-by-valueDoStuff(Date d);//…Date first(1,1,2000);DoStuff(first);what about use of myLength in code as opposed to length()?template <class Item>tvector(const tvector<Item> & vec)// precondition: Item supports assignment // postcondition: return copy of vec { // allocate storage myList = new Item[myLength=vec.myLength]; assert(myList != 0); // copy elements for(int k = 0; k < vec.myLength; k++) { myList[k] = vec.myList[k]; } }CPS 108 : Fall 20024.4Assignment operatorWe want to have deep copy when assigning as well as when we copyObject x(23,4);Object y;y = x; // assignment operatorObject z = x; // copy constructor!!!z = y = x; // how does this work?Assignment operator returns *thisWon’t be const reference return, will be referenceAssignment operator checks for not assigning to selfCan assign to self via aliasing, e.g., pass-by-referenceAssign to every data member (deep copy as needed)See tvector for detailsCPS 108 : Fall 20024.5DestructorIf you need copy constructor, you need assignment operator, and you need destructorWhat is purpose of destructor?Free resourcesWhat’s a resource: memory, files, network connectionsWhen is the destructor called?Automatically when a stack object goes out of scopeWhen you call delete on a heap objectWhat’s the problem with this “automatic destruction”?It’s not automatic, it’s fraught with problems getting it rightWhat about yahoo and the rolling


View Full Document

Duke CPS 108 - Playing well with Java Collections

Download Playing well with Java Collections
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Playing well with Java Collections and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Playing well with Java Collections 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?