Puzzle 7 | All Good Things Must Come to an End |
| #include <iostream> |
| #include <string> |
| |
| struct Connection |
| { |
| Connection(const std::string &name) : name_(name) |
| { |
| std::cout << "Created " << name_ << "\n"; |
| } |
| |
| ~Connection() { |
| std::cout << "Destroyed " << name_ << "\n"; |
| } |
| |
| std::string name_; |
| }; |
| |
| Connection global{"global"}; |
| |
| Connection &get() |
| { |
| static Connection localStatic{"local static"}; |
| return localStatic; |
| } |
| |
| int main() |
| { |
| Connection local{"local"}; |
| Connection &tmp1 = get(); |
| Connection &tmp2 = get(); |
| } |
Guess the Output | |
---|---|
Try to guess ... |
Get C++ Brain Teasers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.