Skip to content

Commit 3c5120d

Browse files
committed
move constructors
1 parent 300c616 commit 3c5120d

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/move_constructors.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,23 @@ class Person {
4848
// and moves the contents of the rvalue passed in as an argument to this
4949
// Person object instance. Note the usage of std::move. In order to ensure
5050
// that nicknames in object person is moved, and not deep copied, we use
51-
// std::move, which casts the lvalue person.nicknames_ to an rvalue, which
52-
// represents the value itself. Also note that I don't call std::move on the
53-
// age_ field. Since it's an integer type, it's too small to incur a
51+
// std::move. std::move will cast the lvalue person.nicknames_ to an rvalue,
52+
// which represents the value itself. Also note that I don't call std::move
53+
// on the age_ field. Since it's an integer type, it's too small to incur a
5454
// significant copying cost. Generally, for numeric types, it's okay to copy
5555
// them, but for other types, such as strings and object types, one should
5656
// move the class instance unless copying is necessary.
5757
Person(Person &&person)
5858
: age_(person.age_), nicknames_(std::move(person.nicknames_)),
5959
valid_(true) {
60+
std::cout << "Calling the move constructor for class Person.\n";
6061
// The moved object's validity tag is set to false.
6162
person.valid_ = false;
6263
}
6364

6465
// Move assignment operator for class Person.
6566
Person &operator=(Person &&other) {
67+
std::cout << "Calling the move assignment operator for class Person.\n";
6668
age_ = other.age_;
6769
nicknames_ = std::move(other.nicknames_);
6870
valid_ = true;
@@ -132,14 +134,16 @@ int main() {
132134
std::cout << "Printing andy1's validity: ";
133135
andy1.PrintValid();
134136

135-
// However, note that because the copy assignment operator and copy
136-
// constructor are deleted, this code will not compile. The first two lines
137-
// use the default constructor and then copy assignment operator and the last line
138-
// uses the copy constructor. Try uncommenting one part at a time to see the
139-
// resulting compiler errors.
137+
// However, note that because the copy assignment operator is deleted, this code
138+
// will not compile. The first line of this code constructs a new object via the
139+
// default constructor, and the second line invokes the copy assignment operator
140+
// to re-initialize andy3 with the deep-copied contents of andy2. Try uncommenting
141+
// these lines of code to see the resulting compiler errors.
140142
// Person andy3;
141143
// andy3 = andy2;
142144

145+
// Because the copy constructor is deleted, this code will not compile. Try
146+
// uncommenting this code to see the resulting compiler errors.
143147
// Person andy4(andy2);
144148

145149
return 0;

0 commit comments

Comments
 (0)