Presentation is loading. Please wait.

Presentation is loading. Please wait.

14.4 Copy Constructors.

Similar presentations


Presentation on theme: "14.4 Copy Constructors."— Presentation transcript:

1 14.4 Copy Constructors

2 Copy Constructors Special constructor used when a newly created object is initialized to the data of another object of same class Default copy constructor copies field-to-field Default copy constructor works fine in many cases

3 Copy Constructors Problem: what if object contains a pointer?
class SomeClass { public: SomeClass(int val = 0) {value=new int; *value = val;} int getVal(); void setVal(int); private: int *value; }

4 Copy Constructors What we get using memberwise copy with objects containing dynamic memory: SomeClass object1(5); SomeClass object2 = object1; object2.setVal(13); cout << object1.getVal(); // also 13 13 object1 object2 value value

5 Programmer-Defined Copy Constructor
Allows us to solve problem with objects containing pointers: SomeClass::SomeClass(const SomeClass &obj) { value = new int; *value = obj.value; } Copy constructor takes a reference parameter to an object of the class

6 Programmer-Defined Copy Constructor
Each object now points to separate dynamic memory: SomeClass object1(5); SomeClass object2 = object1; object2.setVal(13); cout << object1.getVal(); // still 5 5 13 object1 object2 value value

7 Programmer-Defined Copy Constructor
Since copy constructor has a reference to the object it is copying from, SomeClass::SomeClass(SomeClass &obj) it can modify that object. To prevent this from happening, make the object parameter const: SomeClass::SomeClass (const SomeClass &obj)

8

9 MyString Design MyString class: Represent a sequence of characters
the length of the string is important data member: s : pointer len methods: constructor: int n or char * length, print, destructor

10 Declare the Class my_string
class MyString{ public: MyString(int n); MyString(const char * str); ~ MyString(); int length() const ; //const member function void print() const; private: char * s; int len; };

11 Constructors MyString :: MyString(int n) { s = new char[n+1]; s[0] = ‘\0'; len =n; } MyString :: MyString(const char * str) len = strlen(str); s = new char[len+1]; strcpy(s,str);

12 Overload Operator + (MyString) member function
Prototype: MyString operator +(const MyString &b) const; Definition: MyString MyString ::operator+ (const MyString &b) const { cout << "member +" << endl; char * temp = new char[this->len + b.len +1]; strcpy(temp, this->s); strcat(temp, b.s); MyString str(temp); return str; }

13 Returning Local Object from a function
Returning an object invokes the copy constructor while returning a reference doesn't. If a method or function returns a local object, it should return an object, not a reference. It is an error to return a pointer to a local object. Once the function completes, the local object are freed. The pointer would be a dangling pointer that refers to a nonexistent object.


Download ppt "14.4 Copy Constructors."

Similar presentations


Ads by Google