Session_13

Session_13 - Not from Textbook The Copy Constructor In...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Not from Textbook The Copy Constructor In addition to providing a default constructor and destructor, the compiler provides a default copy constructor. The copy constructor is called every time a copy of an object is made. When you pass an object by value, either into a function or as a function’s return value, a temporary copy of that object is made. If the object is a user-defined object, the class’s copy constructor is called . All copy constructors take one parameter, a reference to an object of the same class. It is a good idea to make it a constant reference because the constructor will not have to alter the object passed in. For example, CAT(const CAT & theCat); In the above example, the CAT constructor takes a constant reference to an existing CAT object. The goal of the copy constructor is to make a copy of theCat. The default copy constructor simply copies each member variable from the object passed as a parameter to the member variables of the new object . This is called a member-wise (or shallow) copy, and although this is fine for most member variables, it breaks pretty quickly for member variables that are pointer to objects on the free store. Let’s begin without using the free store: #include <iostream> using namespace std; class CAT { public : CAT(); // default constructor ~CAT(); // default destructor int GetAge() const { return itsAge; } void SetAge ( int age) {itsAge = age; } private : int itsAge; }; CAT::CAT() // default constructor { itsAge = 5; } CAT::~CAT() // default destructor { } void myFunc(CAT boots) { cout << "boot's age: " << boots.GetAge() << endl; cout << "setting boots to 7. ..\n" ; boots.SetAge(7); cout << "boot's age: " << boots.GetAge() << endl; } Page 1 of 15
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
main() { CAT frisky; cout << "frisky's age: " << frisky.GetAge() << endl; myFunc(frisky); // copy constructor used to pass frisky by value cout << "frisky's age: " << frisky.GetAge() << endl; return 0; } Everything is fine in this case. Let’s try to do the same using the free store: #include <iostream> using namespace std; class CAT { public : CAT(); // default constructor ~CAT(); // default destructor int GetAge() const { return *itsAge; } void SetAge ( int age) {*itsAge = age; } private : int *itsAge; }; CAT::CAT() // default constructor { itsAge = new int ; *itsAge = 5; } CAT::~CAT() // default destructor { // left empty for demonstration purpose } void myFunc(CAT boots) { cout << "boot's age: " << boots.GetAge() << endl; cout << "setting boots to 7. ..\n" ; boots.SetAge(7); cout << "boot's age: " << boots.GetAge() << endl; } int main() { CAT frisky; cout << "frisky's age: " << frisky.GetAge() << endl; myFunc(frisky); // copy constructor used to pass frisky by value cout << "frisky's age!!!: " << frisky.GetAge() << endl; return 0; } In this case, we have a problem, as we are using a “shallow copy”. A shallow or member- wise copy copies the exact values of one object’s member variables into another object. Pointers in both objects end up pointing to the same memory.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

Session_13 - Not from Textbook The Copy Constructor In...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online