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

Copy Constructor or Copy Assignment?

easyClasses

📄 Code

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

🎯 Your Answer