LearnFrenzy
Sign in Take a free test

Move Semantics · Exercise 4 of 4

Move Semantics Move Semantics Programs

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

C++ programs that implement and use move semantics: resource classes, move-only handles, efficient builders and forwarding factories. Write each one yourself first, then open the solution – every solution was compiled and run, and the output shown is the real output.

Question 1 Hard

Write IntArray with a raw buffer and all five special members (deep copy, noexcept moves via std::exchange, copy-and-swap assignment), plus push_back with growth; count copies and moves while using it.

Question page & discussion
Question 2 Medium

Write a TempFile class that owns a std::FILE* from tmpfile(), is move-only (copies deleted, noexcept moves), closes in its destructor, and can be stored in a vector and returned from functions.

Question page & discussion
Question 3 Hard

Write template create<T>(Args&&... args) that logs and returns std::make_unique<T>(std::forward<Args>(args)...); show that lvalue arguments are copied and rvalue arguments are moved into the new object.

Question page & discussion
Question 4 Medium

Build a long report by chaining operator+ on std::string temporaries and compare with a version that reuses one buffer with += and reserve; show that the result is the same and how the temporary chain reuses storage.

Question page & discussion
Question 5 Medium

Sort a vector of Record objects that each hold a large vector, counting copies and moves made by std::sort, to show that sorting uses moves (never copies) when the type is movable.

Question page & discussion
Question 6 Medium

Write a MessageQueue holding std::unique_ptr<Message> in a std::deque, with push (taking ownership) and pop (returning ownership or nullptr); producers create messages and a consumer drains the queue.

Question page & discussion
Question 7 Easy

Show that moving a std::vector transfers its buffer (same data() pointer) while copying allocates a new one, and that the moved-from vector is empty and reusable.

Question page & discussion
Question 8 Medium

Write a Point class that counts constructions, copies and moves, then add points to a reserved vector with push_back of a named object, push_back of a temporary, push_back with std::move, and emplace_back; print the counts.

Question page & discussion
Question 9 Medium

Write pipeline stages that take std::string by value, modify it and return it (trim, lower-case, collapse spaces), and chain them so the string's buffer is moved from stage to stage instead of copied.

Question page & discussion
Question 10 Hard

Show the difference between moving a derived object into a base by value (slicing) and moving ownership through std::unique_ptr<Base>; give the hierarchy a virtual clone for copies.

Question page & discussion
All exercises doneBack to Move SemanticsRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links