CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #61

Copy or Move: Which Constructor Runs?

easyClasses

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <utility>
3using namespace std;
4 
5class Tag {
6public:
7 Tag() { cout << "default" << endl; }
8 Tag(const Tag& other) { cout << "copy" << endl; }
9 Tag(Tag&& other) noexcept { cout << "move" << endl; }
10};
11 
12int main() {
13 Tag a;
14 Tag b = a;
15 Tag c = std::move(a);
16 Tag d(std::move(b));
17 Tag e(c);
18 return 0;
19}

🎯 Your Answer