Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is a copy constructor? A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance during initialization.

Similar presentations


Presentation on theme: "What is a copy constructor? A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance during initialization."— Presentation transcript:

1 What is a copy constructor? A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance during initialization. It must have one of the following form MyClass( const MyClass& other ); MyClass( MyClass& other ); MyClass( volatile const MyClass& other ); MyClass( volatile MyClass& other ); If not specified, the compiler will automatically create a default copy constructor. It does a member-wise copy of the source object.

2 Default copy constructor example class MyClass { int x; char c; std::string s; }; Consider the following class The compiler will create a default (or implicit) copy constructor equivalent to: MyClass::MyClass( const MyClass& other ) : x( other.x ), c( other.c ), s( other.s ) {} When the class contain pointer type member variables, the above default constructor is insufficient!

3 Explicitly specify copy constructor example class MyClass { int x; char c; char *s; }; Consider the following class The compiler will create a default (or implicit) copy constructor equivalent to: MyClass::MyClass( const MyClass& other ) : x( other.x ), c( other.c ), s( other.s ) {} When the class contain pointer type member variables, the above default constructor is insufficient! Recall the overloading assignment operator “=“!

4 Explicitly specify copy constructor example class MyClass { int x; char c; char *s; }; Consider the following class Specify your copy constructor as: MyClass::MyClass( const MyClass& other ) : x( other.x ), c( other.c) { s = new char[strlen(other.s)+1]; strcpy (s, other.s); //deep copy } Recall the overloading assignment operator “=“!

5 When the copy constructor is called class MyClass { int x; char c; char *s; }; MyClass::MyClass( const MyClass& other ) : x( other.x ), c( other.c) { s = new char[strlen(other.s)+1]; strcpy (s, other.s); //deep copy } MyClass c_obj1 ( ); //doing some process on c_obj1 MyClass c_obj2(c_obj1); MyClass c_obj3 = c_obj1; // call the copy constructor c_obj3 = c_obj2; // call the assignment operator

6 When the copy constructor is called class MyClass { int x; char c; char *s; }; MyClass::MyClass( const MyClass& other ) : x( other.x ), c( other.c) { s = new char[strlen(other.s)+1]; strcpy (s, other.s); //deep copy } class DClass { // Dclass’s member functions MyClass c_obj; }; DClass::DClass ( const MyClass& other ) : c_obj (other) { } initialized the member objects in the constructor of the container class in object composition

7 When the copy constructor is called The following cases may result in a call to a copy constructor: 1.When an object is returned by value 2.When an object is passed (to a function) by value as an argument 3.When an object is thrown 4.When an object is caught 5.When an object is placed in a brace-enclosed initializer list


Download ppt "What is a copy constructor? A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance during initialization."

Similar presentations


Ads by Google