← Back to Question List
C++ Code Walkthrough #76
A Static Counter Inside operator<<
📄 Code
Read carefully — what does this print?1#include <iostream>2#include <string>3using namespace std;45class Ticket {6 string m_event;7public:8 Ticket(const string& event) : m_event(event) {}9 friend ostream& operator<<(ostream& os, const Ticket& t);10};1112ostream& operator<<(ostream& os, const Ticket& t) {13 static int calls = 0;14 calls++;15 os << "Print " << calls << ": " << t.m_event;16 return os;17}1819int main() {20 Ticket a("Jazz"), b("Rock");21 cout << a << endl;22 cout << a << endl;23 Ticket c = b;24 cout << c << " | " << b << endl;25 return 0;26}