Skip to content

Instantly share code, notes, and snippets.

@macrat
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macrat/81eccba1fa93884636b1 to your computer and use it in GitHub Desktop.
Save macrat/81eccba1fa93884636b1 to your computer and use it in GitHub Desktop.
やったーC言語でクラス書けたよー! ・・・二度と書くもんか。
#include <stdio.h>
#include <stdlib.h>
typedef struct stMY_CLASS{
char *name;
char* (*GetName)(struct stMY_CLASS*);
void (*SetName)(struct stMY_CLASS*, char*);
}MyClass;
char* MyClass_GetName(MyClass* self)
{
return self->name;
}
void MyClass_SetName(MyClass* self, char* name)
{
self->name = name;
}
MyClass* MyClass_new()
{
MyClass* self = (MyClass*)malloc(sizeof(MyClass));
self->GetName = MyClass_GetName;
self->SetName = MyClass_SetName;
return self;
}
int main()
{
MyClass* myclass = MyClass_new();
myclass->SetName(myclass, "hello, world!");
printf("%s\n", myclass->GetName(myclass));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment