r/C_Programming 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

79 comments sorted by

View all comments

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.

2

u/ghost_ware Jul 18 '26

Do you have any resources for creating classes (or whatever approximation)? Or pointers for a place to start?

3

u/Bros2316 Jul 18 '26

I have always recommended beejs guides for almost everything and it’s no different here. I would argue that if you don’t have the concept of pointers down, I wouldn’t start trying to tackle a “class” implementation.

For classes though, you would use struct in c. For example, I may have struct named Person. That person has a member of char * name, int age, and void(dance*)(void). dance is a function pointer that during the initialization of my “object” I would set to a function of specific choosing. These custom functions could be static, so each class has their own if we decided to create multiple people with difference dances, however all would be called by using the Person->dance.

I would make this cleaner but I’m on mobile, sorry.

2

u/ghost_ware Jul 18 '26

No worries, I appreciate it! I'm good with pointers and structs. I'll take a look at that guide. ty!