r/C_Programming • u/Gullible_Ostrich_370 • Jul 17 '26
C with classes
I'm curious to know: who uses some C++ features when coding in C? And what feature(s) are you using?
29
Upvotes
r/C_Programming • u/Gullible_Ostrich_370 • Jul 17 '26
I'm curious to know: who uses some C++ features when coding in C? And what feature(s) are you using?
View all comments
Show parent comment
4
u/Bros2316 Jul 18 '26
Certainly. Responding on mobile, so cant show code directly. Strings, are almost an exact replica of how Windows handles them. You have a struct that contains an underlying pointer that is the actual “string”, then we have members that are the “strings” len and overall capacity. Capacity being how much memory has been allocated to store the string and how large it can grow before realloc is required. I then added functions like append so that you can pass a string into it and it will perform that for you.
As for slices, at the simplest form, it’s just a a constant reference to some X data. I wrote it to be “type safe” but we won’t get into that. Assume in this example we have a buffer of data that is 400 bytes and let’s say 300-320 is what you want a slice of. You would index into that allocated buffer, return the starting address and the “slice” struct will store that address along with the length that the slice contains.
So when the slice is returned to the caller, it will have the address of the “slice” of data you’re after. To be safe, the slice will ensure that you don’t try to access data outside its bounds, etc.