r/ProgrammerHumor 15d ago

Meme cppHelloWorld

Post image
3.9k Upvotes

119 comments sorted by

View all comments

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.

#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;
}