#include <iostream>
#include <string>
class SystemClock {
private:
int time_ticks;
SystemClock():time_ticks(0){}
public:
// TODO: Implement Static method getInstance()
static SystemClock& getInstance() {
static SystemClock instance; //if i do not declare this as static, it will returning a dangling reference to a local dead object
return instance;
}
void tick() {
time_ticks++;
}
int getTime() const {
return time_ticks;
}
};
int main() {
// This line tries to get the global instance
SystemClock& clk = SystemClock::getInstance();
int N;
if (!(std::cin >> N)) return 0;
for (int i = 0; i < N; ++i) {
std::string cmd;
std::cin >> cmd;
if (cmd == "TICK") {
clk.tick(); // accessing dangling reference -> Crash or Garbage
} else if (cmd == "SHOW") {
std::cout << "Time: " << clk.getTime() << std::endl;
}
}
return 0;
}
Input
3 TICK TICK SHOW
Expected Output
Time: 2