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

Pass by Value Copies, Pass by Reference Does Not

mediumFunctions

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <string>
3using namespace std;
4 
5class 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};
12 
13void byValue(Tag t) {
14 cout << "in byValue" << endl;
15}
16 
17void byRef(const Tag& t) {
18 cout << "in byRef" << endl;
19}
20 
21int main() {
22 Tag a("a");
23 byValue(a);
24 byRef(a);
25 cout << "end of main" << endl;
26 return 0;
27}

🎯 Your Answer