Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Move Constructors | Implementing Move Operations
C++ Move Semantics

bookMove Constructors

A move constructor enables an object to take ownership of resources from a temporary (rvalue) object, avoiding expensive deep copies. Its signature uses an rvalue reference.

main.cpp

main.cpp

copy
123456789101112131415161718192021222324252627282930313233343536373839
#include <iostream> #include <cstring> class StringHolder { public: char* data; // Constructor StringHolder(const char* str) { data = new char[std::strlen(str) + 1]; std::strcpy(data, str); std::cout << "Constructed: " << data << std::endl; } // Move constructor StringHolder(StringHolder&& other) noexcept { data = other.data; other.data = nullptr; std::cout << "Move constructed." << std::endl; } // Destructor ~StringHolder() { if (data) std::cout << "Destroyed: " << data << std::endl; delete[] data; else std::cout << "Destroyed: nullptr" << std::endl; } }; int main() { StringHolder a("hello"); StringHolder b(std::move(a)); }

In the example above, the move constructor transfers the resource pointer and nulls out the source, ensuring only one object manages the resource.

A move constructor is called when an object is initialized from a temporary (rvalue) of the same type, such as when returning a local object from a function or using std::move.

After the move, the source object should be left in a valid but unspecified state. Typically, pointers are set to nullptr.

question mark

What is the primary purpose of a move constructor in C++?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

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

bookMove Constructors

Swipe um das Menü anzuzeigen

A move constructor enables an object to take ownership of resources from a temporary (rvalue) object, avoiding expensive deep copies. Its signature uses an rvalue reference.

main.cpp

main.cpp

copy
123456789101112131415161718192021222324252627282930313233343536373839
#include <iostream> #include <cstring> class StringHolder { public: char* data; // Constructor StringHolder(const char* str) { data = new char[std::strlen(str) + 1]; std::strcpy(data, str); std::cout << "Constructed: " << data << std::endl; } // Move constructor StringHolder(StringHolder&& other) noexcept { data = other.data; other.data = nullptr; std::cout << "Move constructed." << std::endl; } // Destructor ~StringHolder() { if (data) std::cout << "Destroyed: " << data << std::endl; delete[] data; else std::cout << "Destroyed: nullptr" << std::endl; } }; int main() { StringHolder a("hello"); StringHolder b(std::move(a)); }

In the example above, the move constructor transfers the resource pointer and nulls out the source, ensuring only one object manages the resource.

A move constructor is called when an object is initialized from a temporary (rvalue) of the same type, such as when returning a local object from a function or using std::move.

After the move, the source object should be left in a valid but unspecified state. Typically, pointers are set to nullptr.

question mark

What is the primary purpose of a move constructor in C++?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1
some-alt