lect11_1

lect11_1 - Copy constructor and this pointer 1 Additional...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Copy constructor and this pointer 1 Additional Class Features - Part 1 Copy constructor and this pointer In C++, we can initialize an object using two slightly different forms of syntax. Consider the following initializations. Initialization-------------- int i = 10; OR int i(10); MyString a = "Hello"; OR MyString a("Hello"); Let us contrast the initialization with the assignment. Assignment---------- int i; i = 10; // value of i is reset to 10 Although initialization looks similar to assignment, it is important to understand that they are two entirely different operations. In the above code, involving a basic type such as an int , it may not make any difference; but when we deal with class types, it does make a big difference. • An initialization is made every time a new object is created. • In an assignment, no new object is created; the value of an existing object is reset. In C++, a type of initialization that closely resembles assignment occurs when one object is initialized using another object of the same class . For example, consider the following declaration. MyString c = a; // alternatively we can write MyString c(a); Look at the statement MyString c = a; carefully. This statement creates a new object c and initializes the value of c to the value of a previously created object a . The constructor that performs this type of initialization is called a copy constructor . If the class does not define a copy constructor, the compiler will provide one for the class. Copy constructor and this pointer 2 The compiler supplied default copy constructor performs memberwise copying between the objects; exactly the way the default assignment operator does for the assignment. Thus, by the declaration MyString c = a; the default copy constructor sets c ’s rep value to a.rep . Like the default assignment operator, default copy constructor is adequate only if the class has no pointer data members. Since MyString class has no copy constructor, the declaration MyString c = a; cre- ates object c as shown below. However, this is not what we want, because any changes made on object c would affect object a . Object c must be independent from a , with the requirement that its initial state (or value) is same as that of a as illustrated below. In order to accomplish this, the MyString class must have its own copy constructor as shown below. MyString(MyString &ob) { // creates an object with C-string as in ob rep = new char[strlen(ob.rep) +1]; strcpy(rep, ob.rep); } // end constructor MyString(MyString &ob) Copy constructor and this pointer 3 Notice that the function header of a copy constructor has the general form class_name(class_name &) As with all constructors, the function name must be the class name. The parameter list contains only one parameter which is a reference to the class object. This is a characteristic of all copy constructors....
View Full Document

This note was uploaded on 04/07/2008 for the course CS 181 taught by Professor Satya during the Fall '08 term at Stevens.

Page1 / 10

lect11_1 - Copy constructor and this pointer 1 Additional...

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

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