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?
56
u/mlugo02 Jul 17 '26
None, I used to wish I had function overloading in C but I have since changed my mind
37
u/dixiethegiraffe Jul 18 '26
Namespaces for organization
16
u/Bros2316 Jul 17 '26
I have recreated several things from c++ and rust that I fell in love with and brought them to C. Examples are “classes”, tokens, channels, vectors, String, Slices, struct pack and unpack, etc.
It makes writing in c that much more enjoyable and allows you to really appreciate and have a deeper understanding of those higher level abstractions those other languages just give you.
11
u/questron64 Jul 17 '26
How do you mean? You're either using C or using C++, you can't just decide to use C++ features in C without leaving C. They are different languages.
14
u/Doug2825 Jul 18 '26
If you have C++ features you aren't using C.
Do you mean approximating C++ classes by making functions take a pointer to an associated struct? Or are you asking about using C++ but limiting yourself to C because of performance/realtime requirements for specific section of code? Because if you have access to full C++ and are limiting yourself to C for mundane parts of the code you are doing bad C++, not C.
3
u/markand67 Jul 18 '26
Can't use C++ feature if using a C compiler so no.
What I really miss from C++ is RAII but with our code base we have made a rule that a fully zero'ed struct should be considered minimal valid for destruction so that you don't have to check whether it was correctly initialized when destructing so a quick exit may look like
2
u/Hammykat-_- Jul 18 '26
It's actually pretty easy to code some features in C that are in C++, like for classes, you can include a pointer inside a struct to a function or another struct. Though, it takes a bit more time. Also, there are things that take much more time, like making lists, dictionaries, etc.
2
u/Forever_DM5 Jul 18 '26
Honestly I just use structs in C++ but I do love the for each loop it’s very nice
2
u/Dry-War7589 Jul 18 '26
I'm currently working on something similar, but it goes further than a couple of macros. Im designing a small extension on top of C that transpiles back to plain C. The idea is that a struct gets a mandatory constructor/destructor that the compiler calls automatically when the object leaves scope, plus methods get tied to the type instead of manually passing self as the first argument everywhere. No inheritance, no vtables, object layout stays the same as a plain struct. Haven't gotten past the spec/paper stage yet so I dont have anything to show, but curious if this would actually be useful to any of you or if you'd just say "I already do that with macros/by hand and it's fine"?
2
u/Coding-Kitten Jul 18 '26
If you're using any C++ features you're by definition not coding in C.
6
u/cincuentaanos Jul 17 '26
There are no "classes" in C, just like there aren't any in a computer's CPU. Many people use object orientation in C but that's just a way to design a program.
1
u/MagicWolfEye Jul 18 '26
- operator overloading for vectors (with that I mean vector maths, not dynamic lists -.-)
- a bit of function overloading or default parameters but rarely and I could probably live without it
- I did a bit of digital boardgame programming and I always used classes + inheritance for the player so to differentiate between a human player that plays with UI, one who plays with std_in and ones that are AI opponents
- I have a templated dynamic_list
1
u/ExtraTNT Jul 18 '26
functional programming go brrr…
1
u/QuirkyXoo Jul 18 '26
All my code is C with classes, even when I don’t actually use them..., lol. I stick to few concepts like encapsulation, to define, hide, and expose methods and data; ctor and dtor for initialization and cleanup, and polymorphism through virtual functions and inheritance, which I often combine. I also use some other "fancy" techniques like raii, while I avoid like the plague things such as namespaces, templates, STL, etc., and all the "modern" C++ features. A minimal, classic approach, I would say.
1
u/thetraintomars Jul 18 '26
Typed enums. And virtual objects for my hardware abstraction layer
1
u/TopiKekkonen Jul 18 '26
I've generally found structs and function pointers to be enough for most cases.
1
u/gunkookshlinger Jul 18 '26
I'm wiritng a kernel and using cpp with basically no libc++ just for nice to haves like templates, constexrp, type traits, some cpp style class/struct stuff for simple private/public data management
1
u/Key_River7180 Jul 18 '26
I think that will error...
None, most C++ is weird
1
u/heavymetalmixer Jul 19 '26
That depends on what features you're talking about, some of them cna be emulated in C in some way (like runtime polymorphism and namespaces), while others directly need another language like C++ (function overloading, templates, constexpr for functions, etc).
Right now I try my best to write C code that works exactly the same inside C++ or at least with veyr minimal changes, so for the most part I don't use C++ exclusive features.
1
u/Evil-Twin-Skippy Jul 20 '26
I do objects with structures and pointers to functions.
But most of my higher level integration is handled by running in an interpreted language. Essentially C is only for writing optimized functions.
1
u/ConstantElegant5781 Jul 21 '26
I'm currently retired, but even while working by default, I'd create utilities in what I called "C with STL". To use the STL classes, I had to use a C++ compiler, but I'd restrict myself to using C-style procedural programming. I used to be a kernel engineer, and so I know how to create all the standard data structures from scratch. Multiple times I've spent most of a week creating a semi-balanced binary tree, plus all its test cases, but with STL I just have to use map. Far less work for me and those reviewing my code and just as efficient as what I could code by hand. Only gain inefficiencies if I were to derive a class from another class, which I don't do while using "C with STL".
1
u/Conscious_Buddy1338 Jul 22 '26
For me it was interesting to know that you can use some OOP stuff in C. Instead of methods from C++, you can declare the pointer to function in C structure. And you can call the functions pointed by this pointer via syntax like you call the method of class in C++.
1
u/Independent_Art_6676 Jul 23 '26
a long time c++ coder, the first stand-out for me is probably a bizarre choice. Operator overloading.
nothing sucks more than turning z = a*(b+c); into C code that looks like tmp = typeadd(b,c); z = typemul(a,tmp); its unreadable from a 'math guy' standpoint for even simple stuff, and try doing complicated equations written in C++ overloaded math that looks just like on-paper math this way, its horrid.
and it seeps into you. I would miss string and vector right off. It would be awfully nice if C had a standardized set of DSA containers so we did not have to deal with joe's tree and bob's linked list that are in completely different styles and have totally different approaches. I know there are libraries that do that to an extent, but it always seems like something went awry on that front esp in older projects.
1
u/Anode1_dev Aug 04 '26
Objects are not necessary. They are an obstacle to write efficient code. Functions and algorithms should be first, streams, structures are enough. When we think preliminary about objects, we bind ourselves to not the most efficient data flow, sacrifice efficiency and data locality
0
u/papertowelroll17 Jul 17 '26
I code in C++ for a living and very rarely do I make classes tbh. I wouldn't rank that high on the list of things that C could use from C++.
Of course classes do enable some things that are essential (smart pointers and the like), but very rarely do I create a new one.
-5
u/smichaele Jul 18 '26
If you're using classes, you're coding in C++, by definition.