3

I'm looking for some kind of C dialect that is as minimalistic as C but has built-in classes support. So I can (and encouraged to) use macros, pointers to arrays and manual memory management but also create classes, add fields and member functions to them etc. This question appeared when I tried to implement some kind of OOP in C and typedef struct and function pointers do something similar to what I want, but "member functions" require to manually pass a pointer to the object as a parameter to them, and that's not what I want to do. I know that I can just write on C++ as on "C with classes" and I would, however C++ encourages a different programming style and I'm curious if there is something that is exactly what I want.

I was searching for "C with classes" but I've only seen C++ in results, so I expect that the answer is "just use C++" and I'm OK with that, but I'm just curious.

ryyker
  • 22,849
  • 3
  • 43
  • 87
Sun of A beach
  • 171
  • 1
  • 10
  • There is also `C#`, which I am reticent to say, I guess qualifies as a type of `C`. But it is anything but minimalist. – ryyker Jul 26 '22 at 14:03
  • Related: [Improving a minimalistic OOP for microcontrollers using C, gcc, C99, and Macros with optimization](https://stackoverflow.com/questions/29909665/improving-a-minimalistic-oop-for-microcontrollers-using-c-gcc-c99-and-macros) – Weather Vane Jul 26 '22 at 14:03
  • 1
    Check out at least *Objective C*, *D* and the new *Carbon*, for more insight maybe. – hyde Jul 26 '22 at 14:05
  • 1
    Re "*C++ encourages a different programming style*", yeah, but you're obviously ignoring that recommendation?! So what's the issue? – ikegami Jul 26 '22 at 14:06
  • I think D and Carbon are supposed to aim for feature parity with C++, so calling them _"as minimalistic as C"_ seems a bit of a reach – Useless Jul 26 '22 at 14:09
  • Nothing prevents you to use C++ like C with classes. – Jabberwocky Jul 26 '22 at 14:13
  • Carbon is 7 days old! I will definitely check it out. Thanks @hyde – Zakk Jul 26 '22 at 14:47

1 Answers1

6

C++ encourages a different programming style

You can write C++ in whatever style you like. Just choose not to use the features (and libraries) that don't suit your C-with-classes aesthetic.

"C with classes" was originally compiled to C by Cfront, but that's extremely dead AFAIK.

I doubt there's much demand for a resurrected Cfront when simply choosing a subset of C++, and using a current C++ compiler, already does everything you actually require.


FWIW I have written object-oriented C in the past, and manually passing this isn't that much of a burden. Even in Python you have to declare the self parameter explicitly, and nobody seems upset about that. Having to pass it in explicitly as well isn't so bad.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • So, you have successfully described a language _"...that is not C++"_, but is :-) – ryyker Jul 26 '22 at 14:16
  • 1
    I've previously used _C++ without exceptions_ (for reasons of varying soundness), _C++ without templates_ (on a compiler with terrible template support in the late 90s), _C++ without virtual inheritance_ by default if our class hierarchies are sane ... choosing a suitable subset of the language for the domain is normal practice IME. – Useless Jul 26 '22 at 14:18
  • Once you start using C++ you will reach for another feature, and another, and another. And that's a good thing - you'll be using them because you want to – user253751 Jul 26 '22 at 14:35