2 ms·
You don’t need all that to emulate objects in C. Instead use this recipe: - classes <-> compilation units - objects <-> structs - methods <-> functions recei
by G3rn0ti 3y ago
You don’t need all that to emulate objects in C. Instead use this recipe:
- classes <-> compilation units
- objects <-> structs
- methods <-> functions receiving a pointer to a struct as first argument; use name of the class as prefix e.g. Point3D_scale(Point3D *self, double factor)
- constructors are just methods and initialize the struct
- destructors are just methods and free all memory occupied by the struct
Inheritance is a bit more difficult this way but as others pointed out already it is way over used IMHO. But if you really want to do that you could design a child class by using a struct pointing to the parent struct and using that to manually delegate all inherited method calls to the parent. You could probably write a fancy macro automating this delegation for you.