Question 8
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
#include <iostream>
class Logger {
Logger() {}
public:
static Logger& instance() { static Logger l; return l; }
void log(const char* m) { std::cout << m << '\n'; }
};
int main() {
Logger mine;
mine.log("hi");
return 0;
}
- A log must be static
- B The constructor is private (a singleton design), so main cannot create a Logger; use Logger::instance().log("hi") Correct
- C instance must return a pointer
- D Logger needs a destructor
Correct Answer: Option B
Private constructors restrict object creation to the class itself – here to the single static instance.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!