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

Counting Copies and Moves Into a Parameter

hardFunctions

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <string>
3#include <utility>
4using namespace std;
5 
6class Tag {
7 string m_name;
8public:
9 Tag(const string& name) : m_name(name) { cout << "ctor " << m_name << endl; }
10 Tag(const Tag& other) : m_name(other.m_name) { cout << "copy " << m_name << endl; }
11 Tag(Tag&& other) noexcept : m_name(std::move(other.m_name)) {
12 other.m_name = "moved";
13 cout << "move " << m_name << endl;
14 }
15 ~Tag() { cout << "dtor " << m_name << endl; }
16};
17 
18void take(Tag t) {
19 cout << "take" << endl;
20}
21 
22int main() {
23 Tag a("a");
24 take(a);
25 take(std::move(a));
26 cout << "end" << endl;
27 return 0;
28}

🎯 Your Answer