← Back to Question List
C++ Code Walkthrough #64
Counting Copies and Moves Into a Parameter
📄 Code
Read carefully — what does this print?1#include <iostream>2#include <string>3#include <utility>4using namespace std;56class 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};1718void take(Tag t) {19 cout << "take" << endl;20}2122int main() {23 Tag a("a");24 take(a);25 take(std::move(a));26 cout << "end" << endl;27 return 0;28}