objects

objects in c
git clone https://noulin.net/git/objects.git
Log | Files | Refs | README | LICENSE

commit 2428e341dda9eb15c247da933e0c179659f8f2d4
parent 5d521040b8e29ce7154c631c43f1bd4fb5d8aab9
Author: Remy Noulin (Spartatek) <remy.noulin@spartatek.se>
Date:   Tue, 21 Feb 2017 13:49:22 +0100

basic C objects

README.md |  7 +++++++
main.c    | 45 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+)

Diffstat:
AREADME.md | 7+++++++
Amain.c | 45+++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,7 @@ +C objects + +Run: + +gcc -ggdb main.c +gdb -tui a.out + diff --git a/main.c b/main.c @@ -0,0 +1,45 @@ +#include <stdlib.h> +#include <stdio.h> + +void print(char *s) { + printf(s); +} + +typedef struct class classt; +typedef void (*printt)(char *string); +typedef void (*terminateClasstt)(classt **self); + +struct class { int a; printt print; terminateClasstt terminate;}; + +#define createclasst(obj) classt obj; initiateclasst(&obj) +#define createAllocateclasst(obj) classt *obj; initiateAllocateclasst(&obj) +void initiateclasst(classt *self); +void terminateclasst(classt **self); + +void initiateAllocateclasst(classt **self) { + + (*self) = malloc(sizeof(classt)); + initiateclasst(*self); +} + +void initiateclasst(classt *self) { + + self->terminate = terminateclasst; + self->a = 0; + self->print = print; +} + +void terminateclasst(classt **self) { + + free((*self)); +} + +void main() { + + createclasst(obj1); + obj1.print("\nRegular Object\n"); + + createAllocateclasst(obj2); + obj2->print("\nObject in heap\n"); + obj2->terminate(&obj2); +}