Last active
August 29, 2015 14:22
-
-
Save macrat/81eccba1fa93884636b1 to your computer and use it in GitHub Desktop.
やったーC言語でクラス書けたよー! ・・・二度と書くもんか。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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