← Back to Question List
C++ Code Walkthrough #56
Pass by Value Copies, Pass by Reference Does Not
📄 Code
Read carefully — what does this print?1#include <iostream>2#include <string>3using namespace std;45class Tag {6 string m_name;7public:8 Tag(const string& name) : m_name(name) { cout << "ctor " << m_name << endl; }9 Tag(const Tag& other) : m_name(other.m_name) { cout << "copy " << m_name << endl; }10 ~Tag() { cout << "dtor " << m_name << endl; }11};1213void byValue(Tag t) {14 cout << "in byValue" << endl;15}1617void byRef(const Tag& t) {18 cout << "in byRef" << endl;19}2021int main() {22 Tag a("a");23 byValue(a);24 byRef(a);25 cout << "end of main" << endl;26 return 0;27}