Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Copy Constructor

C++ Copy Constructor

C++ Classes & Objects C++ Classes & Objects

The copy constructoris a special constructor that, when creating an object, uses a previously created object of the same class to initialize the newly created object. The copy constructor is typically used for:

If no copy constructor is defined in the class, the compiler defines one itself. If the class has pointer variables and uses dynamic memory allocation, it must have a copy constructor. The most common form of a copy constructor is as follows:

classname

(

const

classname

&

obj

)

{

// Body of the constructor

}

Here,obj obj is an object reference used to initialize another object.

Example

#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

;

}

;

// Member function definition, including constructor

Line

::

Line

(

int

len

)

{

cout

<<

"

Call the constructor

"

<<

endl

;

// Allocate memory for the pointer

ptr

=

new

int

; *

ptr

=

len

;

}

Line

::

Line

(

const

Line

&

obj

)

{

cout

<<

"

Call the copy constructor and allocate memory for pointer ptr

"

<<

endl

;

ptr

=

new

int

; *

ptr

= *

obj

.

ptr

;

// Copy values

}

Line

::~

Line

(

void

)

{

cout

<<

"

Free memory

"

<<

endl

;

delete

ptr

;

}

int

Line

::

getLength

(

void

)

{

return

ptr

;

}

void

display

(

Line

obj

)

{

cout

<<

"

line size :

"

<<

obj

.

getLength

(

)

<<

endl

;

}

// main function of the program

int

main

(

)

{

Line

line

(

10

)

;

display

(

line

)

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



调用构造函数

调用拷贝构造函数并为指针 ptr 分配内存

line 大小 : 10

释放内存

释放内存

The example below makes a slight modification to the above example, initializing the newly created object by using an existing object of the same type:

Example

#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

;

}

;

// Member function definition, including constructor

Line

::

Line

(

int

len

)

{

cout

<<

"

Call the constructor

"

<<

endl

;

// Allocate memory for the pointer

ptr

=

new

int

; *

ptr

=

len

;

}

Line

::

Line

(

const

Line

&

obj

)

{

cout

<<

"

Call the copy constructor and allocate memory for pointer ptr

"

<<

endl

;

ptr

=

new

int

; *

ptr

= *

obj

.

ptr

;

// Copy values

}

Line

::~

Line

(

void

)

{

cout

<<

"

Free memory

"

<<

endl

;

delete

ptr

;

}

int

Line

::

getLength

(

void

)

{

return

ptr

;

}

void

display

(

Line

obj

)

{

cout

<<

"

line size :

"

<<

obj

.

getLength

(

)

<<

endl

;

}

// main function of the program

int

main

(

)

{

Line

line1

(

10

)

;

Line

line2

=

line1

;

// The copy constructor is also called here

display

(

line1

)

;

display

(

line2

)

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



调用构造函数

调用拷贝构造函数并为指针 ptr 分配内存

调用拷贝构造函数并为指针 ptr 分配内存

line 大小 : 10

释放内存

调用拷贝构造函数并为指针 ptr 分配内存

line 大小 : 10

释放内存

释放内存

释放内存

C++ Classes & Objects C++ Classes & Objects