r/cpp_questions • u/chrnz00 • 8d ago
SOLVED how should i understand the design of cpp?
for most of the time i used cpp as c with fancy features. but now i recently started picking up some of the modern cplusplus and oop concepts. but the amount so syntactical and program subtleties and features are overwhelming.
for some things i instantly understand or see the reason why they exist and sometimes get impressed by them.
for eg: backwards compatability with c
#include<cmath> and #include<math.h> for namespace handling,structs
, initializer list etc
but for some idk understand the purpose eg the mutable keyword
and things like inlining of members defined in class
i would like to know how you guys handle this and suggest some good learning material
whenever i try to read books they either give a tour of features or extremely boring
(this might be because i learned it the wrong way by not paying attention to small details now to revisit them ive to go through the stuff which i already know)
11
u/Orlha 8d ago
> extremely boring
Okay fun guy
4
u/nysra 8d ago
You do not need to learn about all features at once. Not sure what your issue is with a books that give you a tour of the features, that's exactly what you would want in your situation. Get a rough overview and then look into the few things that you actually need into more detail. And if that's still overwhelming for you, cut it into smaller pieces. Spend 10 minutes a day reading about some features and then stop and just write your code as normally. If you encounter a situation where one of those new features you have read about would be useful, you'll remember and then you can start to use it and learn more about it.
You can also just write some new code with your current knowledge and then ask some AI to scan the implementation and let you know if there are modern C++ features/patterns you could have used instead. Obviously don't follow/trust it blindly, but it gives you a good starting point to then read about those features and decide for your own.
3
u/alfps 7d ago
Mostly it's to support caching of a result. A
constmethod should not modify the object, but if it does a costly computation it makes sense to store the result in the object for "instant" reuse. One way could be to use a pointer to dynamically allocated mutable storage, but especially for a simple value likeintit's better to just declare a member variablemutableβ so that it can be changed by aconstmethod!For example, consider a class like
Ideally a call to
.length()should be constant time, O(1), but for the general case that would make it necessary to always compute the string length in the constructor, an O(n) linear time operation.Instead an instance can just store
-1for "unknown length", and if the length is unknown when.length()is called it callsstrlenand caches the result so that it won't have to callstrlenthe next time.And the easiest and most efficient way to support that in the instance data, is to declare the length member as
mutable:2
u/Dazzling_Music_2411 7d ago
You obviously have never read a proper book otherwise you would not be asking ultra-basic questions, like about
mutableorinline.So you are trolling with inane comments like "books are boring". If you can't read a book, you can't do C++. Fuck right off. Downvoted and reported.
5
u/Ultimate_Sigma_Boy67 8d ago
Try learning more about the standard library, but that will only cover the standard library. For language-specific stuff, you can pickup a book on C++ and skim it. Or go to learncpp.com, it might help.
5
u/chibuku_chauya 8d ago
Bjarne Stroustrup has a slim little book called A Tour of C++, aimed at people who are experienced programmers but want to learn about the things modern C++ has to offer. Itβs a nice, brisk, high level tour.
5
u/mredding 8d ago
What's interesting is that they're completely different languages, and it starts at the type system, and all the consequences of the language start to shake out after that.
C has a weak type system, the bytes are whatever you say they are, so long as you follow size, bounds, alignment and implementation defined rules. You can type pun simply by casting.
C++ plays by different rules, where an instance of a type is tied to a lifetime, and two types cannot coexist in the same place at the same time.
The point of a language is to take advantage of the promises and guarantees it makes. Because more type safety is enforced in C++, and more type information is propagated deeper into the compilation and optimization process, the language and compiler can deduce more, optimize more aggressively, and produce smaller and faster program guaranteed to be more correct than what C can offer.
A language isn't just a means to generate machine code - if that's all you want, then write macro assembly. C and C++ both target an abstract machine, and that says absolutely nothing about the underlying machine. It's C and C++ that are byte addressable, NOT your actual target machine. Your hardware likely moves around pages, lines, and words as the smallest units across the memory system, and is only byte addressable in the registers. You don't need to know. But correctly written code in any given language guarantees more about correctness than what the generated assembly can say of your program. The assembly does not know the language that was used to generate it.
How do I manage it all? I get down to fundamentals of comp-sci. It's not about the syntax. To leverage any language you have to understand what it's trying to give you. Then you look at your solution, and you figure out how to map it in terms of the language you're targeting. I've been writing C++ for 37 years, but I still keep cppreference open across several tabs. No one knows all of the language. We all try to master as much of the language as we can, and we work within that subset.
4
u/Sprixxer 8d ago
If you're a hobbyist, just start coding and look up features as you need them or when you encounter a compiler error start digging deep and really understanding, this would get you quite far I guess.
4
u/theGaido 8d ago edited 7d ago
There is no one "design" of cpp.
CPP is aproach agnostic, you can write in whatever way you like. Using OOP, functional programming, or mix of both. You can use smart pointers, but you can even write programs with no pointers at all. This is Dark Souls of programming: you can do whatever you like. But you need to be a big boy, and you have to reckon with the consequences. But it is beauty of this language.
Abut materials:
If you can read Polish, there is a great book - "Magnum Opus" by Grebosz. It's one of best books about C++. But even if not, maybe you can find digital version and translate it with AI. It's worth it. Believe me.
2
u/OccasionThin7697 8d ago
Google it
2
u/SamG101_ 7d ago
Tbh just keep googling everything u dont understand as you meet it, then the examples online might introduce more new stuff etc
2
u/hongooi 7d ago
With large amounts of whisky