r/cpp • u/Tcshaw91 • Nov 16 '25
Wait c++ is kinda based?
Started on c#, hated the garbage collector, wanted more control. Moved to C. Simple, fun, couple of pain points. Eventually decided to try c++ cuz d3d12.
-enum classes : typesafe enums -classes : give nice "object.action()" syntax -easy function chaining -std::cout with the "<<" operator is a nice syntax -Templates are like typesafe macros for generics -constexpr for typed constants and comptime function results. -default struct values -still full control over memory -can just write C in C++
I don't understand why c++ gets so much hate? Is it just because more people use it thus more people use it poorly? Like I can literally just write C if I want but I have all these extra little helpers when I want to use them. It's kinda nice tbh.
View all comments
Show parent comment
1
u/DonBeham Nov 16 '25
This is partially true. If you use STL containers, then these manage the memory (shared_ptr and unique_ptr are also containers). But if you do allocation yourself and create an object on the heap using
newthen eventually you need to calldelete. Nobody will do the delete for you.Here are several problems:
RAII doesn't protect you from leaking
pand it doesn't protect you from accessing destroyed objects likes. With a GCpwill eventually be deleted and in C#swould either be copied (if it was a value type) or it would outlive the scope offun(if it was a reference type).And here the C++ classic and how non-obvious is it to have an invalid reference:
In C# the list contains only references (pointers) and thus the object is located in a totally different place. It's memory location never changes. This is convenient for the programmer, but has the drawback that cache locality is bad.
shared_ptralso adds some convenience, but also has quite a performance penalty over a raw pointer orunique_ptr.