Lecture 11

Copy Assignment Operator

EXAMPLE:

Student billy {...};
Student bobby {billy}; /// copy ctor
Student jane;
jane = billy; // copy assignment
jane.operator = (billy);

n1=n2; // n1.operator=(n2);
n1=n2=n3; // n2.operator = (n3);

EXAMPLE:

Node & Node::operator=(const Node &other) {
  if (this==&other) return *this; // self assignment check
  data = other.data;
  
  delete next; // since we are updating on existing obj
  
  next = other.next? new Node {*other.next}=nullptr;
  return *this;
}
  • If new fails, stop executing

    • next is a "dangling pointer"

New assignment operator:

Copy and Swap Idiom

EXAMPLE: Classes/vvalue/node.cc

Note: we would like 2 kinds of constructors:

  1. One that copies from non-temp object (copy ctor) (lvalue reference)

  2. One that steals from a temp obj are r-values (rvalue reference)

Rvalue Reference: Reference to a temporary Node <- Node Node & <- lvalue Node && <- rvalue reference

Move Constructor

Move Assignment Operator

Last updated

Was this helpful?