Copy constructor

A copy constructor, often called copy constructor in object-oriented programming, a special constructor that takes a reference to an object of the same type as a parameter and has the task to create a copy of the object.

Example

An example is a class that handles a string or a class of the same type through its constructor. The following example in C shows for comparison an ordinary constructor and a copy constructor:

Class MitCopyKonstruktor { private:    char * cString;   public:    / / Ordinary constructor    MitCopyKonstruktor (const char * value)    {      cString = new char [ strlen (value) 1]; Reserve / / memory of the correct length      strcpy ( cString, value); / / Copy the string from value in the reserved memory    }      / / Copy constructor:    / / In C always have the signature " class name (const classname & ) "    MitCopyKonstruktor (const MitCopyKonstruktor & rhs ) / / Usually, rhs: " Right Hand Side"    {      cString = new char [ strlen ( rhs.cString ) 1];      strcpy ( cString, rhs.cString );    } }; call

The copy constructor is called during the initialization of an object by another object of the same type. In C this other object is passed as the only parameter to the constructor. It takes place in the declaration of the object, the assignment of another object or the object is passed as the value parameter to a function or method.

Example in C ( continued):

Int main () { MitCopyKonstruktor mitCC ( " Dulz "); / / Create a string MitCopyKonstruktor mitCC2 = mitCC; / / Copy constructor, assignment syntax MitCopyKonstruktor mitCC3 ( mitCC ); / / Copy constructor call syntax } use

Some programming languages, such as C , provide a pre-defined copy constructor available that just copies the member variables of the copied object into the object to be initialized. ( In other programming languages ​​, such as Java, the copy constructor must be explicitly programmed. ) However, this can lead to problems. Are the member variables namely handles to resources and returns the already existent object frees up the resources, so the handle is not valid in the object created by default copy constructor and its use can lead to program crashes. Pointer to memory areas are also copied, so that the copy of the original object now has a pointer to an unused memory areas. Now, if these memory areas changed, eg by a change of origin or of the copied object, it has the potential to affect all objects that use the pointer to the same memory area.

In the example, each instance of string contains its own memory that is allocated when calling the copy constructor. If each copy an object has exclusive access to its resources, that is, they do not have to share with other objects, it is called a deep copy (English deepCopy ). Otherwise, it is called a shallow copy ( engl. shallow copy). A shallow copy produced by the compiler with the pre-defined copy constructor automatically. Is in the class string no copy constructor defined that creates a deep copy for a copy two objects would have a pointer to the same memory block as the address would simply be copied. An object but then do not know if the other already delete has called on the memory block. Both an access to the memory and re- delete would then lead to a crash of the program. The following example illustrates this.

Example in C ( abridged):

Class ZeichenketteF { public:    ZeichenketteF (): m_memory (NULL ) { } / / default constructor    ZeichenketteF (const char * value) / / constructor    {      m_memory = new char [ strlen (value) 1]; Reserve / / memory of the correct length      strcpy ( m_memory, value); / / Copy the string value of the reserved memory    }    / / The class ZeichenketteF assumes the default copy constructor   private:    char * m_memory; };   void fail () {    ZeichenketteF name ( " Wolfgang" );      / / Anonymous block for new Scope:    {      ZeichenketteF copy ( name); / / Now create a so-called [ [ shallow copy .]]      / / Both name.m_memory and kopie.m_memory now show the same memory!    } / / Destructor for automatic copy, are memory pointed to by m_memory, free      / / Name the object also refers to - a now shared - memory area   } / / Destructor for automatic name.    / / The attempt to free the same memory block again leads (perhaps only ) to crash Costs are lower copies

As seen by the example below call, held deep copies, it follows a certain load. To avoid unnecessary load to recommend two variants of the copy shown above strategy.

  • Use of resources by means of reference counting in different instances together; many implementations of the String class to make use of this.
  • To take const references as parameters in functions and methods, in all the cases in which only read parameters.

The copy itself shows in his prototype how unnecessary deep copies of objects avoids, on the one must have read access only: He assumes a constant reference, otherwise he would have to (implicitly) be called before it is called! The signature " class name (const classname & ) " is therefore also typical.

202195
de