DOC PREVIEW
FSU COP 3330 - Copy Constructor

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 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 8 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 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Copy Constructors Fall 2008Automatic FunctionsCopy ConstructorSlide 4Slide 5Slide 6Slide 7Copy constructorCopy ConstructorsFall 2008 Dr. David A. [email protected] Functions• Functions automatically created for each class: –Constructor: Creates objects–Destructor: Deletes objects–Copy Constructor–Assignment operator = We have covered Constructor and Destructor.Copy Constructor•Copy constructor is a “constructor”•It is a function with the same name as the class and no return type. •However, it is invoked implicitly–An object is defined to have the value of another object of the same type. –An object is passed by value into a function–an object is returned by value from a functionCopy Constructor•ExamplesFraction f1, f2(3,4)Fraction f3 = f2; // Copy Const.f1 = f2; // Not a copy but assignment // operator.Copy Constructor•Declaring and Defining–A copy constructor always has one (1) parameter, the original object. –Must be the same type as the object being copied to. –Always passed by reference (must be because to pass by value would invoke the copy constructor). –Copy constructor not requiredFraction (const Fraction &f);Timer (const timer & t);Copy Constructor•Shallow copy vs deep copy–The default version is a shallow copy. I.E. the object is copies exactly as is over to the corresponding member data in the new object location. –Example: •Fraction f1(3,4);•The object example illustrates the definition of an object f1 of type Fraction. •If passed as a parameter, a shallow copy will be sufficient.Copy Constructor•When there is a pointer to dynamic data, a shallow copy is not sufficient. •Why? Because a default or shallow copy will only copy the pointer value (Address). Essentially both objects are pointing to the same item. Here we need a deep copy.Copy constructor•Deep CopyDirectory::Directory (const Directory & d){ maxsize = d.maxsize; currentsize = d.currentsize; entryList = new Entry[d.maxsize]; for (int i=0; i<currentsize; i++) entryList[i] =


View Full Document

FSU COP 3330 - Copy Constructor

Download Copy Constructor
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 Copy Constructor 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 Copy Constructor 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?