Lecture 4 - Copy Constructor Flashcards

1
Q

Copy constructor

A

When a copy of an object is made, it is initialized using a special constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

3 situations in which copy constructors are called

A
  1. assignment from one object to another at time of creation
  2. passing an object by value to a function
  3. returning an object by value from a function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Default copy constructor

A
simply copies values from one object to another
//SHALLOW COPY
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Shallow copy

A

copying values as they are from one object to another

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Syntax of custom copy constructor

A

NewClass::NewClass (const NewClass & obj)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the issue with shallow copy?

A

If one of the data members is a reference type, the address is copied BUT the memory location is not
○ Both the original object and the copy point to the same memory location, which can lead to logical errors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Deep copy

A

Creating a new object and copying the values of all data members AND making new copies of all referred objects
The copied object shares nothing with the old object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly