Hacker News new | ask | show | jobs
by mothsonasloth 1259 days ago
As a Java dev who learned C++ at university the hate towards C++ is misplaced.

C++ is a no thrills language which lets you have freedom but freedom to also shoot yourself in the foot 100+n ways, but this gives you a good foundation as a developer IMHO.

Every time I think about garbage collection in Java, I can appreciate that I am not having to write code to destruct an object or worry about passing references incorrectly.

3 comments

For Java'eans, here's an interesting StackOverflow question about why there's no Garbage Collector in C++ and what it means:

https://stackoverflow.com/q/147130/1593077

and my particular answer:

https://stackoverflow.com/a/48046118/1593077

in essence, with Modern C++, it is relatively easy to just not produce garbage which needs to be collected :-)

I've been getting back into C++ after an extremely long hiatus and I've been impressed by how thoroughly modern it is. The worst crime you're likely to commit these days is accidentally invoking a copy constructor more often than you want to, and even then, not actually creating leaks.

In fact, the most striking thing coming from a language like Java or C# isn't memory management at all, is how utterly non-OO C++ actually is, especially the standard library. I understand how key the zero-cost tenet is in C++, but it's very alien to be picturing an interface and thinking you'd like an Iterable<char> or IEnumerable<char> there, but having to learn about iterators and traits and concepts etc. To be honest, there are equally many functional affordances in C++ as OO ones at this point, but the native core is generic programming, which is very idiosyncratic.

Even when C++ was created, object-orientation was not a goal, but rather a means to an end. Yes, that was a significant feature, and the original name was "C with objects", but even then it was only part of the bigger picture, and that part got smaller over time. The next big step away from an OO-orientation of the language (no, that's not a typo) was when the STL was adopted into the standard library - with its iterators and templated mini-algorithms. At that point it could certainly be said already that C++ is a multi-paradigmatic language, and that's become even more true over time.

You could definitely (?) implement a Java-like container framework, with class like Iterable<char> or IEnumerable<char> and almsot every OO bell and whistle - if you wanted to. But it would not be very useful.

C with Classes, not C with Objects, was the original name of what became C++.
> The worst crime you're likely to commit these days is accidentally invoking a copy constructor more often than you want to, and even then, not actually creating leaks.

As much as I like modern C++, use after free is still way too easy to commit. But if you stick to value types and mostly don't store references/non-owning raw pointers, then it's OK.

I switched from C++ to Java too. Still, every time I type `new MyObject()` without a corresponding `delete` I feel like I got away with some sort of crime ;)
in c++ you probably never want to write 'new MyObject()' at all. perhaps you didn't learn c++ from a good book?
Depends more on when you learned C++. C++11 didn’t immediately replace C++98…
to whoever downvoted this, why would you write 'new MyObject()'?
I didn’t downvote and I don’t know C++, but I thought that the new and delete operators are how you manage dynamic memory allocation.

Is it not the way?

no, it really isn't. mostly you don't need to manage objects dynamically at all (make them automatic) but if you do, use something like std::make_unique(...)
I haven't used Java since school so I'm curious about modern Java. Do you need to worry about circular references preventing garbage collection or are there easy tools to detect such cases?
Circular references have never prevented garbage collection in any version of Java AFAIK.
Java will collect sets of objects with circular references: https://stackoverflow.com/questions/1910194/how-does-java-ga...