Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Move Semantics and std::move | Smart Pointers and Memory Management
Quizzes & Challenges
Quizzes
Challenges
/
C++ Modern Features

bookMove Semantics and std::move

Move semantics are a powerful feature in modern C++ that allow you to transfer resources from one object to another without unnecessary copying. This is achieved using rvalue references, which are a type of reference that can bind to temporary objects (also known as rvalues). The std::move utility is used to cast an object to an rvalue reference, signaling that the resources of the object can be moved rather than copied. This is especially useful for classes that manage dynamic memory or other resources, as it enables efficient transfer of ownership, avoiding redundant allocations and deallocations.

main.cpp

main.cpp

copy
1234567891011
#include <iostream> #include <string> int main() { std::string original = "Hello, World!"; std::string moved_to = std::move(original); std::cout << "moved_to: " << moved_to << std::endl; std::cout << "original (after move): " << original << std::endl; }

While this simple example demonstrates moving a string, the real power of move semantics becomes clear when used in user-defined classes that manage dynamic resources. Implementing move constructors and move assignment operators can significantly improve performance by reusing existing allocations instead of duplicating them.

main.cpp

main.cpp

copy
123456789101112131415161718192021222324252627
#include <iostream> class Buffer { public: Buffer(size_t size) : size_(size), data_(new int[size]) { std::cout << "Allocated " << size_ << " ints\n"; } // Move constructor Buffer(Buffer&& other) noexcept : size_(other.size_), data_(other.data_) { other.data_ = nullptr; // Transfer ownership std::cout << "Moved buffer\n"; } ~Buffer() { delete[] data_; } private: size_t size_; int* data_; }; int main() { Buffer buf1(10); Buffer buf2 = std::move(buf1); // Move, not copy }

This example shows how resources can be efficiently transferred using a move constructor. After the move, buf1 no longer owns its data, preventing double deletion while avoiding unnecessary copying.

question mark

When a move constructor is used, what happens to the original object’s resource pointer?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 14.29

bookMove Semantics and std::move

Stryg for at vise menuen

Move semantics are a powerful feature in modern C++ that allow you to transfer resources from one object to another without unnecessary copying. This is achieved using rvalue references, which are a type of reference that can bind to temporary objects (also known as rvalues). The std::move utility is used to cast an object to an rvalue reference, signaling that the resources of the object can be moved rather than copied. This is especially useful for classes that manage dynamic memory or other resources, as it enables efficient transfer of ownership, avoiding redundant allocations and deallocations.

main.cpp

main.cpp

copy
1234567891011
#include <iostream> #include <string> int main() { std::string original = "Hello, World!"; std::string moved_to = std::move(original); std::cout << "moved_to: " << moved_to << std::endl; std::cout << "original (after move): " << original << std::endl; }

While this simple example demonstrates moving a string, the real power of move semantics becomes clear when used in user-defined classes that manage dynamic resources. Implementing move constructors and move assignment operators can significantly improve performance by reusing existing allocations instead of duplicating them.

main.cpp

main.cpp

copy
123456789101112131415161718192021222324252627
#include <iostream> class Buffer { public: Buffer(size_t size) : size_(size), data_(new int[size]) { std::cout << "Allocated " << size_ << " ints\n"; } // Move constructor Buffer(Buffer&& other) noexcept : size_(other.size_), data_(other.data_) { other.data_ = nullptr; // Transfer ownership std::cout << "Moved buffer\n"; } ~Buffer() { delete[] data_; } private: size_t size_; int* data_; }; int main() { Buffer buf1(10); Buffer buf2 = std::move(buf1); // Move, not copy }

This example shows how resources can be efficiently transferred using a move constructor. After the move, buf1 no longer owns its data, preventing double deletion while avoiding unnecessary copying.

question mark

When a move constructor is used, what happens to the original object’s resource pointer?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2
some-alt