Last active
March 11, 2018 06:14
-
-
Save segfo/1a9edffdb688d0ca85bff0036e3a331b 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
// タグ名だけ書く。 | |
struct Context; | |
// ステートを管理するためのContext構造体を引数に持ってコールバックを呼びたい | |
// 例えば非同期関数がエラーだった時とかのコールバックや | |
// オブザーバーパターンを実装するときとかによく使うやつ。 | |
typedef struct Callback | |
{ | |
uintmax_t state; //状態の保存 | |
void (*onError)(struct Context* ctx); //ここでContextを渡す関数ポインタを定義する | |
} Callback; | |
// 実際にメンバを定義する | |
// なんかいろいろな状態を保存しておくための構造体。 | |
typedef struct Context | |
{ | |
int state0; | |
char *state1; | |
const struct Callback* stateCallback; //ここでも参照 | |
} Context; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment