120 likes | 698 Views
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 );
E N D
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( constMyClass& other ); MyClass( MyClass& other ); MyClass( volatileconstMyClass& other ); MyClass( volatileMyClass& other ); If not specified, the compiler will automatically create a default copy constructor. It does a member-wise copy of the source object.
Default copy constructor example Consider the following class classMyClass { • int x; • char c; • std::string s; }; The compiler will create a default (or implicit) copy constructor equivalent to: MyClass::MyClass( constMyClass& other ) : x( other.x ), c( other.c ), s( other.s ) {} When the class contain pointer type member variables, the above default constructor is insufficient!
Explicitly specify copy constructor example Consider the following class classMyClass { • int x; • char c; • char *s; }; The compiler will create a default (or implicit) copy constructor equivalent to: MyClass::MyClass( constMyClass& 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 “=“!
Explicitly specify copy constructor example Consider the following class classMyClass { • int x; • char c; • char *s; }; Specify your copy constructor as: MyClass::MyClass( constMyClass& 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 “=“!
When the copy constructor is called classMyClass { • int x; • char c; • char *s; }; MyClass::MyClass( constMyClass& other ) : x( other.x ), c( other.c) { • s = new char[strlen(other.s)+1]; • strcpy (s, other.s); //deep copy } MyClass c_obj1 (<initial values>); //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
When the copy constructor is called classMyClass { • int x; • char c; • char *s; }; classDClass { • // Dclass’s member functions • MyClassc_obj; }; MyClass::MyClass( constMyClass& other ) : x( other.x ), c( other.c) { • s = new char[strlen(other.s)+1]; • strcpy (s, other.s); //deep copy } • DClass::DClass( constMyClass& other ) • : c_obj (other) • { } initialized the member objects in the constructor of the container class in object composition
When the copy constructor is called • The following cases may result in a call to a copy constructor: • When an object is returned by value • When an object is passed (to a function) by value as an argument • When an object is thrown • When an object is caught • When an object is placed in a brace-enclosed initializer list