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