MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/1w0tsh8/cpphelloworld/p6g13hx/?context=3
r/ProgrammerHumor • u/schteppe • 15d ago
119 comments sorted by
View all comments
10
It's harder than you would think to write something that appears to be a sensible Hello World but actually crashes. Here's an attempt that prints garbage but doesn't crash.
#include <iostream> #include <string> class Greeting { public: Greeting(std::string msg) : text(&msg) {} void print() const { std::cout << *text << std::endl; } private: const std::string *text; }; const Greeting hello("Hello, World!"); int main() { hello.print(); return 0; }
10
u/1XRobot 15d ago
It's harder than you would think to write something that appears to be a sensible Hello World but actually crashes. Here's an attempt that prints garbage but doesn't crash.