1 / 7

What is a copy constructor?

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 );

pabla
Télécharger la présentation

What is a copy constructor?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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( 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.

  2. 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!

  3. 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 “=“!

  4. 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 “=“!

  5. 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

  6. 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

  7. 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

More Related