Sei sulla pagina 1di 10

C++ Copy Constructor

Acopy constructoris called whenever a new variable is


created from an object.

Copy Constructor: Uses


1. Initialize one object from another of the same type.
2. Copy an object to pass it as an argument to a function.
3. Copy an object to return it from a function.
.If a copy constructor is not defined in a class, the
compiler itself defines one.If the class has pointer
variables and has some dynamic memory allocations,
then it is a must to have a copy constructor.

// Main function for the program


int main( )
{
int* p = new int;
Ashallow copyof an object
int* q;
copies all of the member field
*p = 100;
q = p;
values. This works well if the fields
return 0;
are values, but may not be what
}

Shallow vs Deep Copies

you want for fields that point to


dynamically allocated memory.
The pointer will be copied. but the
memory it points to will not be
copied -- the field in both the
original object and the copy will
then point to the same

// Main function for the program


int main( )
{
int* p = new int;
int* q = new int;
*p = 100;
*q = *p - 1;
return 0;
}

Shallow vs Deep Copies


Adeep copycopies all
fields,andmakes copies
of dynamically allocated
memory pointed to by the
fields. To make a deep
copy, you must write a
copy constructor and
overload the assignment
operator, otherwise the
copy will point to the
original, with disastrous

The most common form of copy


constructor is shown here:
classname (const classname &obj)
{
// body of constructor
}

Here,objis a reference to an object that is being used to


initialize another object.

Copy Constructor: An Example-function


argument
#include <iostream>
using namespace std;
class Line
{
public:
int getLength( void );
Line( int len ); // simple constructor
Line( const Line &obj); // copy
constructor
~Line(); // destructor
private:
int *ptr;
};

getLength
()
Line(int)
Line(const
Line&)
~Line()
ptr

int

Copy Constructor: An Example-function


argument
// Member functions definitions including
constructor
Line::Line(int len) {
cout << "Normal constructor allocating
ptr" << endl; // allocate memory for the
pointer;
ptr = new int; *ptr = len;
}
Line::Line(const Line &obj) {
cout << "Copy constructor allocating
ptr." << endl;
ptr = new int;
*ptr = *obj.ptr; // copy the value
}
Line::~Line(void) {
cout << "Freeing memory!" << endl;
delete ptr;
}
int Line::getLength( void ) {

getLength
()
Line(int)
Line(const
Line&)
~Line()
ptr

int

Copy Constructor: An Example-function


argument
void display(Line obj)
{
cout << "Length of line : " <<
obj.getLength() <<endl;
}
// Main function for the program
int main( )
{
Line line(10);
display(line);
return 0;
}

When the above code is compiled


and executed, it produces the

getLength
()
Line(int)
Line(const
Line&)
~Line()
ptr

Normal constructor allocating ptr


Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!

int

Copy Constructor: An Example-initialize


variable
getLeng
th()
Line(int
)
Line(const
Line&)
~Line()
// Main function for the program
ptr
int main( )
{
Line line1(10);
Line line2 = line1; // This also calls copy
constructor
display(line1);
display(line2);
return 0;
}

When the above code is compiled


and executed, it produces the

10
int

getLeng
th()
Line(int
)
Line(const
Line&)
~Line()

10
ptr

Normal constructor allocating ptr


Copy constructor allocating ptr.
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!
Freeing memory!

int

w/o Copy Constructor: An Exampleinitialize variable


// Main function for the program
int main( )
{
Line line1(10);
Line line2 = line1; // This will not call a
copy constructor
display(line1);
display(line2);
getLeng
return 0;
th()
}
Line(int

getLeng
th()
Line(int
)
Line(const
Line&)
~Line()

)
Line(const
Line&)
~Line()
ptr

10

ptr

int

Line1

Line2

Potrebbero piacerti anche