Copy constructor
This presentation is the property of its rightful owner.
Sponsored Links
1 / 24

Copy Constructor PowerPoint PPT Presentation


  • 80 Views
  • Uploaded on
  • Presentation posted in: General

Copy Constructor. An overloaded constructor When an object is passed to a function, a bitwise (exact) copy of that object is made and given to the function. If the object contains a pointer to allocated memory, the copy will point to the memory as does the original object. Copy Constructor.

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Copy constructor

Copy Constructor

  • An overloaded constructor

  • When an object is passed to a function, a bitwise (exact) copy of that object is made and given to the function.

  • If the object contains a pointer to allocated memory, the copy will point to the memory as does the original object.


Copy constructor1

Copy Constructor

  • If the copy makes a change to the contents of this memory, it will be changed for the original object too.

  • Also, when the function terminates, the copy will be destroyed, causing its destructor to be called.

  • That can free dynamically allocated memory, used by the original object as well.


What happens

What happens . . .

  • When a function is called that uses pass by value for a class object of DynArray type?

2000

DynArray

?

?

75

?

?

Private:

size 5

arr 2000

~DynArray

DynArray

ValueAt

Store

CopyFrom


Passing a class object by value

Passing a Class Object by Value

// FUNCTION CODE

void SomeFunc( DynArray someArr )

// Uses pass by value

{

.

.

.

.

}

17


By default pass by value makes a shallow copy

By default,Pass-by-value makes a shallow copy

DynArray beta(5); // CLIENT CODE

.

.

.

SomeFunc( beta ); // function call

beta someArr

2000

DynArray

.

.

.

DynArray

.

.

.

?

?

75

?

?

Private:

size 5

arr 2000

Private:

size 5

arr 2000

shallow copy


Suppose somefunc calls store

SupposeSomeFunccalls Store

// FUNCTION CODE

void SomeFunc( DynArray someArr )

// Uses pass by value

{

someArr.Store(290, 2);

.

.

.

}

WHAT HAPPENS IN THE SHALLOW COPY SCENARIO?

19


Beta arr 2 has changed

beta.arr[2] has changed

DynArray beta(5); // CLIENT CODE

.

.

.

SomeFunc( beta);

beta someArr

2000

DynArray

.

.

.

DynArray

.

.

.

?

?

290

?

?

Private:

size 5

arr 2000

Private:

size 5

arr 2000

shallow copy


Beta arr 2 has changed1

beta.arr[2] has changed

NOTICE THAT NOT JUST FOR THE SHALLOW COPY,

BUT ALSO FOR ARGUMENT beta,

THE DYNAMIC DATA HAS CHANGED!

beta someArr

2000

DynArray

.

.

.

DynArray

.

.

.

?

?

290

?

?

Private:

size 5

arr 2000

Private:

size 5

arr 2000

shallow copy


Classes with data member pointers need

Classes with Data Member Pointers Need

CONSTRUCTOR

COPY CONSTRUCTOR

DESTRUCTOR


Shallow copy vs deep copy

Shallow Copyvs.Deep Copy

  • a shallow copycopies only the class data members, and does not make a copy of any pointed-to data

  • a deep copycopies not only the class data members, but also makes a separate stored copy of any pointed-to data


What s the difference

What’s the difference?

  • a shallow copyshares the pointed to dynamic data with the original class object

  • a deep copymakes its own copy of the pointed to dynamic data at different locations than the original class object


Making a separate deep copy

Making a (Separate) Deep Copy

beta

someArr

2000

DynArray

.

.

.

?

?

75

?

?

Private:

size 5

arr 2000

4000

DynArray

.

.

.

?

?

75

?

?

Private:

size 5

arr 4000

deep copy


Assignment and initialization

Assignment and Initialization

  • In both cases the value of one object is given to another.

  • Copy constructor only applies to initializations.


Initialization of class objects

Initialization of Class Objects

  • Initialization can occur three ways:

    • an object is used to initialize another in a declaration statement,

    • passing an object argument by value to a function,

    • returning a temporary object as the return value of a function,

  • by default, C++ uses shallow copies for these initializations


Default copy constructor

default copy constructor

  • C++ automatically provides a default copy constructor that simply duplicates the object.

  • It is possible to specify precisely how one object will initialize another by defining a copy constructor.

  • Copy constructor do not affect assignment operations.


Common form of copy constructor

Common form of Copy Constructor

classname(const classname &obj)

{ ...

}

  • Here obj is a reference to an object that is being used to initialize another object.

    Time t1=t2; // explicitly initializing t1

    func1(t2); // t2 passed as a parameter

    t2=func1(); // t2 receiving a returned object

    In the first two cases, a reference to t2 would be passed to the copy constructor.

    In the third, a reference to the object returned by funct2() is passed to the copy constructor.


As a result

As a result . . .

  • when a class has a data member pointer to dynamically allocated data, you should write what is called acopy constructor

  • the copy constructor is implicitly called in initialization situationsandmakes a deep copy of the dynamic data in a different memory location


More about copy constructors

More about Copy Constructors

  • when there is a copy constructor provided for a class, the copy constructor is used to make copies for pass by value

  • you do not call the copy constructor

  • like other constructors, it has no return type

  • because the copy constructor properly defines pass by value for your class, it must use pass by reference in its definition


Copy constructor2

Copy Constructor

  • copy constructor is a special member function of a class that is implicitly called in these 3 situations:

    • passing object parameters by value

    • initializing an object variable in its declaration

    • returning an object as the return value of a function


Copy constructor

SomeFunc(beta);// copy-constructor

// beta passed by value

beta someArr

2000

4000

DynArray

DynArray

?

?

75

?

?

?

?

75

?

?

Private:

size 5

arr 2000

Private:

size 5

arr 4000

~DynArray

~DynArray

DynArray

DynArray

ValueAt

ValueAt

Store

Store

CopyFrom

CopyFrom

DEEP COPY


Copy constructor

DynArray::DynArray( const DynArray& otherArr )

// Copy constructor

// Implicitly called for deep copy in initializations.

// POST: If room on free store THEN

// new array of size otherArr.size is created

// on free store && arr == its base address

// && size == otherArr.size

// && arr[0..size-1] == otherArr.arr[0..size-1]

// ELSE error message.

{

int i ;

size = otherArr.size ;

arr = new int[size] ; // allocate memory for copy

for ( i = 0; i< size ; i++ )

arr[i] = otherArr.arr[i] ; // copies array

}

34


What about the assignment operator

What about the assignment operator?

  • the default method used for assignment of class objects makes a shallow copy

  • if your class has a data member pointer to dynamic data, you should write a member functionto create a deep copy of the dynamic data


Copy constructor

gamma.CopyFrom(beta);

gamma beta

3000

2000

DynArray

DynArray

?

?

75

?

?

?

?

75

?

?

Private:

size 5

arr 3000

Private:

size 5

arr 2000

~DynArray

~DynArray

DynArray

DynArray

ValueAt

ValueAt

Store

Store

CopyFrom

CopyFrom

DEEP COPY


Copy constructor

void DynArray::CopyFrom ( /* in */ DynArray otherArr )

// Creates a deep copy of otherArr.

// POST: Array pointed to by [email protected] deallocated

// && IF room on free store

// THEN new array is created on free store

// && arr == its base address

// && size == otherArr.size

// && arr[0..size-1] == otherArr[0..size-1]

// ELSE halts with error message.

{

int i ;

delete [ ] arr ;// delete current array

size = otherArr.size ;

arr = new int [size] ;// allocate new array

for ( i = 0; i< size ; i++ ) // deep copy array

arr[i] = otherArr.arr[i] ;

}

37


  • Login