🛠️ Steven Gong

Search

SearchSearch

Feb 23, 2025, 1 min read

Copy-and-Swap Idiom

Learned in CS247.

We use the Copy-and-Swap Idiom to address potential code duplication between copy constructor and copy assignment operator.

Used in the Copy Assignment Operator (after copy constructor is implemented)

Node& Node::operator=(const Node& other) {
	Node tmp{other};
	std::swap(data, tmp.data);
	std::swap(next, tmp.next);
	return *this;
}

There may be memory leaks here!

https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom

Graph View

Backlinks

  • CS247 - Software Engineering Principles
  • Copy Assignment Operator
  • Copy-and-Swap Idiom
  • Copy-on-Write Idiom
  • Exception Safety

Created with Quartz, © 2025

  • Blog
  • LinkedIn
  • Twitter
  • GitHub