Copy Constructors and Deep vs Shallow Copy in C++, Study notes of Object Oriented Programming

The concept of copy constructors in C++ and the difference between deep and shallow copy. It covers the use of copy constructors for initialization, passing arguments to functions, and returning objects from functions. The document also provides an implementation of a copy constructor for a LinkedBag class and discusses efficiency considerations.

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

gaurishaknar
gaurishaknar 🇺🇸

3.4

(8)

232 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Copy Constructor
!1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Copy Constructors and Deep vs Shallow Copy in C++ and more Study notes Object Oriented Programming in PDF only on Docsity!

Copy Constructor

! 1

Copy Constructor

1. Initialize one object from another of the same type

MyClass one; MyClass two = one;

More explicitly

MyClass one; MyClass two(one); // Identical to above.

2. Copy an object to pass by value as an argument to a function

void MyFunction(MyClass arg) { /* ... */ }

3. Copy an object to be returned by a function

MyClass MyFunction() {

MyClass mc;

return mc;

! 2

Creates a new object
as a copy of another one
Compiler will provide one
but may not appropriate
for complex objects

Deep vs Shallow Copy

! 4

Overloaded operator=

MyClass one;

//Stuff here

MyClass two = one;

IS DIFFERENT FROM

MyClass one, two;

//Stuff here

two = one;

! 5 Instantiation: copy constructor is called

Assignment, NOT

instantiation: no constructor

is called, must overload

operator= to avoid

shallow copy

orig_chain_ptr
new_chain_ptr

// Copy first node head_ptr_ = new Node(); head_ptr_->setItem(orig_chain_ptr->getItem()); // Copy remaining nodes Node* new_chain_ptr = head_ptr_; // Points to last node in new chain orig_chain_ptr = orig_chain_ptr->getNext(); Deep vs Shallow Copy

! 8

orig_chain_ptr
new_chain_ptr

Deep vs Shallow Copy // Copy first node head_ptr_ = new Node(); head_ptr_->setItem(orig_chain_ptr->getItem()); // Copy remaining nodes Node* new_chain_ptr = head_ptr_; // Points to last node in new chain orig_chain_ptr = orig_chain_ptr->getNext();

orig_chain_ptr
new_chain_ptr

Deep vs Shallow Copy while (orig_chain_ptr != nullptr) { // Get next item from original chain ItemType next_item = orig_chain_ptr->getItem(); //Create a new node containing the next item Node* new_node_ptr = new Node(next_item); //Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr); // Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); / Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); }

orig_chain_ptr
new_chain_ptr

Deep vs Shallow Copy while (orig_chain_ptr != nullptr) { // Get next item from original chain ItemType next_item = orig_chain_ptr->getItem(); //Create a new node containing the next item Node* new_node_ptr = new Node(next_item); //Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr); // Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); / Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); }

orig_chain_ptr
new_chain_ptr

Deep vs Shallow Copy while (orig_chain_ptr != nullptr) { // Get next item from original chain ItemType next_item = orig_chain_ptr->getItem(); //Create a new node containing the next item Node* new_node_ptr = new Node(next_item); //Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr); // Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); / Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); }

orig_chain_ptr
new_chain_ptr

Deep vs Shallow Copy while (orig_chain_ptr != nullptr) { // Get next item from original chain ItemType next_item = orig_chain_ptr->getItem(); //Create a new node containing the next item Node* new_node_ptr = new Node(next_item); //Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr); // Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); / Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); }

! 16

orig_chain_ptr
new_chain_ptr

Deep vs Shallow Copy while (orig_chain_ptr != nullptr) { // Get next item from original chain ItemType next_item = orig_chain_ptr->getItem(); //Create a new node containing the next item Node* new_node_ptr = new Node(next_item); //Link new node to end of new chain new_chain_ptr->setNext(new_node_ptr); // Advance pointer to new last node new_chain_ptr = new_chain_ptr->getNext(); / Advance original-chain pointer orig_chain_ptr = orig_chain_ptr->getNext(); }

Efficiency Considerations

Every time you pass or return an object by value:

- Call copy constructor

- Call destructor

For linked chain:

- Traverse entire chain to copy ( n “ steps ”)

- Traverse entire chain to destroy ( n “ steps ”)

Preferred:

myFunction(const MyClass& object);

! 17