![]() |
VOOZH | about |
A move constructor is a special constructor in C++ that lets us transfer the contents of one object to another without copying the data. It is useful for performance - it's faster than copying.
Constructor called Move Constructor called After move: obj1: No data obj2: Value: 42 Destructor deleting data: 42 Destructor called on nullptr
Move constructors are used to transfer resources (like memory or file handles) from one object to another without making a copy, which makes the program faster and more efficient. They're especially useful when working with temporary objects or large data.
Constructor is called for 10 Constructor is called for 10 Copy Constructor is called - Deep copy for 10 Destructor is called for 10 Constructor is called for 20 Constructor is called for 20 Copy Constructor is called - Deep copy for 20 Constructor is called for 10 Copy Constructor is called - Deep copy for 10 Destructor is called for 10 Destructor is called for 20 Destructor is called for 10 Destructor is called for 20
Constructor is called for 10 Move Constructor for 10 Destructor is called for nullptr Constructor is called for 20 Move Constructor for 20 Constructor is called for 10 Copy Constructor is called -Deep copy for 10 Destructor is called for 10 Destructor is called for nullptr Destructor is called for 10 Destructor is called for 20
A noexcept move constructor is a move constructor that guarantees it won't throw any exceptions.
Move constructor Move constructor Resize happens Move constructor Copy constructor Copy constructor
Move constructor Move constructor Resize happens Move constructor Move constructor Move constructor