Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Using std::move | Implementing Move Operations
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ Move Semantics

bookUsing std::move

The utility std::move is an essential tool in modern C++ that enables move semantics. It is a standard library function that casts an lvalue (an object with a name and address) to an rvalue reference. By doing so, it allows move constructors or move assignment operators to be invoked, rather than their copying counterparts. However, it's important to understand that std::move does not move anything by itself—it simply makes it possible for move operations to occur.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
#include <iostream> #include <utility> #include <string> class Buffer { public: Buffer(size_t size) : size_(size), data_(new char[size]) { std::cout << "Constructed Buffer of size " << size_ << "\n"; } // Move constructor Buffer(Buffer&& other) noexcept : size_(other.size_), data_(other.data_) { other.data_ = nullptr; other.size_ = 0; std::cout << "Moved Buffer\n"; } // Move assignment operator Buffer& operator=(Buffer&& other) noexcept { if (this != &other) { delete[] data_; data_ = other.data_; size_ = other.size_; other.data_ = nullptr; other.size_ = 0; std::cout << "Move-assigned Buffer\n"; } return *this; } // Destructor ~Buffer() { delete[] data_; std::cout << "Destroyed Buffer\n"; } // Deleted copy constructor and assignment Buffer(const Buffer&) = delete; Buffer& operator=(const Buffer&) = delete; void print() const { std::cout << "Buffer at " << static_cast<void*>(data_) << " size " << size_ << "\n"; } private: size_t size_; char* data_; }; int main() { Buffer a(10); Buffer b(20); std::cout << "Before move:\n"; a.print(); b.print(); // Move assignment: b takes resources from a b = std::move(a); std::cout << "After move:\n"; a.print(); b.print(); }

Use std::move when you no longer need the original object and want to transfer its resources. Do not use it on objects you intend to use afterward.

question mark

Which statements about std::move are correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookUsing std::move

Swipe um das Menü anzuzeigen

The utility std::move is an essential tool in modern C++ that enables move semantics. It is a standard library function that casts an lvalue (an object with a name and address) to an rvalue reference. By doing so, it allows move constructors or move assignment operators to be invoked, rather than their copying counterparts. However, it's important to understand that std::move does not move anything by itself—it simply makes it possible for move operations to occur.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
#include <iostream> #include <utility> #include <string> class Buffer { public: Buffer(size_t size) : size_(size), data_(new char[size]) { std::cout << "Constructed Buffer of size " << size_ << "\n"; } // Move constructor Buffer(Buffer&& other) noexcept : size_(other.size_), data_(other.data_) { other.data_ = nullptr; other.size_ = 0; std::cout << "Moved Buffer\n"; } // Move assignment operator Buffer& operator=(Buffer&& other) noexcept { if (this != &other) { delete[] data_; data_ = other.data_; size_ = other.size_; other.data_ = nullptr; other.size_ = 0; std::cout << "Move-assigned Buffer\n"; } return *this; } // Destructor ~Buffer() { delete[] data_; std::cout << "Destroyed Buffer\n"; } // Deleted copy constructor and assignment Buffer(const Buffer&) = delete; Buffer& operator=(const Buffer&) = delete; void print() const { std::cout << "Buffer at " << static_cast<void*>(data_) << " size " << size_ << "\n"; } private: size_t size_; char* data_; }; int main() { Buffer a(10); Buffer b(20); std::cout << "Before move:\n"; a.print(); b.print(); // Move assignment: b takes resources from a b = std::move(a); std::cout << "After move:\n"; a.print(); b.print(); }

Use std::move when you no longer need the original object and want to transfer its resources. Do not use it on objects you intend to use afterward.

question mark

Which statements about std::move are correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3
some-alt