Object Oriented Programming in ANSI C. Basic example showing how OOP can be realized on ANSI C for Figure class.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
578 B

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #ifndef __OBJECT_H__
  2. #define __OBJECT_H__
  3. typedef struct Object
  4. {
  5. struct Object_vtable
  6. {
  7. /* Object methods */
  8. const char* (*type) ();
  9. struct Object* (*clone) (const struct Object *this);
  10. void (*destroy) (struct Object *this);
  11. } *vtable;
  12. } Object;
  13. static inline const char* Object_type (const Object *this)
  14. {
  15. return this->vtable->type ();
  16. }
  17. static inline Object* Object_clone (const Object *this)
  18. {
  19. return this->vtable->clone (this);
  20. }
  21. static inline void Object_destroy (Object *this)
  22. {
  23. this->vtable->destroy (this);
  24. }
  25. #endif // __OBJECT_H__