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?
27
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
7
u/WittyStick Jul 18 '26 edited Jul 18 '26
There's more than one way you can do it.
Most techniques take advantage of the fact that struct members are ordered, so casting a pointer from one struct to another that share the same initial members is valid in C.
Given:
It is valid to write the following:
Note that in the case of the downcast, if the original value being pointed to was not created as a
derived, but as abase, and you cast toderived, although the cast is valid, attempting to access any data beyond the shared initial members is undefined behavior.This would be the equivalent of attempting to downcast in C++ when the type wasn't created as the derived type.
Upcasts are always safe, but downcasts depend on the dynamic (latent) type - we don't necessarily know if the dynamic cast is correct at compile time.
Using the above, we can make the first member of a struct a pointer to a vtable.
When multiple inheritance is permitted the problem is more difficult - we have multiple vtable pointers.
C does not provide RAII, but it is possible to simulate using extensions such as GCC's
cleanupattribute.There are also ways to implicitly pass
this, rather than the explicitthisas first parameter to every "method" - some approaches just use a macro to do this, but we can also do something closer to C++.The way this is typically done in C++ is
thisgets passed implicitly in a hardware register on a method call - eg,rcxon x86-64 (SYSV convention).We can also simulate such passing of
thisimplicitly via embedded assembly - however, we can't usercxbecause it is part of the calling convention for regular arguments (SYSV). We can take another register which isn't part of a regular call - eg,r10, which is the "static chain pointer", and repurpose it as the object pointer - we just need to restore it when we're done.Note that using the static chain pointer may interfere with other features - notably
__builtin_call_*or__builtin_apply, so you should avoid such features if using it this way. The static chain pointer is usually used for closures (eg, calling Go closures from C) - but closures and objects are basically two different ways of doing the same thing. Implementing closures in C is very similar to implementing objects.The key difference is that a closure holds its function pointer(s) directly and has a pointer to the captured context (data).
Whereas an object holds its data directly and has a pointer to its methods (vtable of function pointers).
So we can see why there's essentially an equivalence between objects and closures - but closures are typically "one function, different data" and objects are typically "one data, different functions".
Closures are a bit more complicated than the above because what we really need is one context per static scope (a frame), as
xandymay come from different static scopes. Each context basically has a pointer to the parent scope. When we accessxfrom the closure, it's the nearestxto it in the chain - which may shadow anyxin parent scopes.