Skip to content

Instantly share code, notes, and snippets.

@cybrox
Last active August 29, 2015 14:11
Show Gist options
  • Save cybrox/a93e36b17bbc2f746824 to your computer and use it in GitHub Desktop.
Save cybrox/a93e36b17bbc2f746824 to your computer and use it in GitHub Desktop.
Simple, object oriented implementation in native C (very basic object handling)
/**
* Simple example of object oriented implementation in native C
* Compiled using the Visual Studio 2013 C/C++ compiler.
*
* Written 2014 by Sven Marc 'cybrox' Gehring
* This is an open example, use it however you want.
*/
#include <stdio.h>
// We need to first declare all the type definitions of the instance structs
// so we can reuse these inside the structs and the method declarations!
typedef struct storageInstance storageInstance;
// We then need to declare all the methods for the respective classes, so we
// can refer to them inside the raw instance struct.
struct storageInstance * storageClassNew();
void storageClassDrop(storageInstance * this);
void storageInstanceOutputValue(storageInstance * this);
void storageInstanceaddNumber(storageInstance * this, int add);
// The storageClass is the pool that will hold all instances of the storage class
// This is not the actual class, but it's named like that so .new() makes sense.
struct storageClassTag {
struct storageInstance * (*new)();
void (*drop)(storageInstance * this);
} storageClass = {
.new = storageClassNew,
.drop = storageClassDrop
};
// The storageInstance is the actual Class struct for storages. This contains all the
// values and linked class methods that can be used in every instance.
struct storageInstance {
int value;
void (*output)(storageInstance * this);
void (*addNumber)(storageInstance * this, int add);
};
// Let's write an example program that creates two instances and increments one of
// them to show that they act completely independet from each other.
int main(void){
storageInstance * myStorageOne = storageClass.new();
storageInstance * myStorageTwo = storageClass.new();
myStorageOne->value = 0;
myStorageTwo->value = 0;
myStorageOne->output(myStorageOne); // 0
myStorageTwo->output(myStorageTwo); // 0
printf("\n");
myStorageOne->addNumber(myStorageOne, 50);
myStorageTwo->addNumber(myStorageTwo, 20);
myStorageTwo->addNumber(myStorageTwo, 5);
myStorageOne->output(myStorageOne); // 50
myStorageTwo->output(myStorageTwo); // 25
printf("\n");
storageClass.drop(myStorageTwo);
myStorageOne->output(myStorageOne); // 50
//myStorageTwo->output(myStorageTwo); // This will crash the programm 'cause it doesn't exist.
getch();
}
// This will allocate space for a new storage class instance on the heap and
// return its pointer to be used as a way to access this class instance
struct storageInstance * storageClassNew(){
struct storageInstance* self = (struct storageInstance*) malloc(sizeof(struct storageInstance));
// We need to register all the methods here, this
// is like the internal instance constructor.
self->output = storageInstanceOutputValue;
self->addNumber = storageInstanceaddNumber;
return self;
}
// This is the deconstructor, it will do all the garbage collection for the class
void storageClassDrop(storageInstance * this){
free(this);
}
// This is an example method that belongs to the storage class. It will output the value
void storageInstanceOutputValue(storageInstance * this){
printf("%d\n", this->value);
}
// This is an example method that belongs to the storage class. It will add the given number to value
void storageInstanceaddNumber(storageInstance * this, int add){
int temp = this->value;
this->value = temp + add;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment