Last active
August 29, 2015 14:03
-
-
Save hsk/00074599d0edff3b0e09 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
extern "C" { | |
void printi(int i) { | |
printf("%d\n", i); | |
} | |
// 型クラスA | |
typedef void (*A_p)(struct A*); | |
struct A { | |
static void _new(A* self) { | |
self->p=A::_p; | |
} | |
A_p p; | |
static void _p(A* self) { | |
printi(1); | |
}; | |
}; | |
// Cデータ型 | |
struct C { | |
int i; | |
static void _new(C* self,int i) { | |
self->i = i; | |
} | |
}; | |
// Cへ型クラスAの実装 | |
struct CA { | |
A a; | |
C* c; | |
static void _new(CA* self, C* c) { | |
self->c = c; | |
// aを初期化 | |
A::_new(&self->a); | |
// aの関数を書き換える | |
self->a.p = CA::_p; | |
} | |
static void _p(A* self) { | |
// C内のiを出力する | |
printi(((CA*)self)->c->i); | |
}; | |
}; | |
void aa(A* a) { | |
a->p(a); | |
} | |
int main() { | |
// Aの型クラスを作成 | |
A a; | |
// 初期化 | |
A::_new(&a); | |
// 実行 | |
a.p(&a); | |
// Cを作成 | |
C c; | |
// 初期化 | |
C::_new(&c, 123); | |
// CをAで包むデータを作成 | |
CA ca; | |
// 初期化 | |
CA::_new(&ca, &c); | |
// 包まれた状態で実行 | |
ca.a.p(&ca.a); | |
// 関数に渡す | |
aa(&a); | |
// caでも動く | |
aa(&ca.a); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment