Cでの設計の質問に問題があります。
引数の数が異なる、かなりの数の関数があるとしましょう。
POQ:
int print_one(int x)
{
printf("one: %d\n", x);
return 1;
}
int print_three(int x, int y, int z)
{
printf("three: %d-%d-%d\n", x, y, z);
return 3;
}
ここで、いくつかのプロパティを構造内のこれらの関数に接続して、パラメーター数を含む正確な関数を知らなくてもそれらを操作できるようにします(構造インターフェイスと呼ぶこともあります)
私はそれをこのように試しました(そして私はかなり間違っていると思います):
typedef int (*pfunc)(int c, ...);
typedef struct _stroffunc
{
pfunc myfunction;
int flags;
int some_thing_count;
int arguments[10];
int argumentcount;
} stroffunc;
int main()
{
stroffunc firststruct;
firststruct.pfunc = (pfunc) print_two;
firststruct.something_count = 101;
arguments[0] = 102;
argumentcount = 1;
flag &= SOME_SEXY_FLAG;
// now I can call it, in a pretty ugly way ... however I want (with patially random results ofc)
firststruct.pfunc(firststruct.arguments[0]);
firststruct.pfunc(firststruct.arguments[0], 124, 11);
firststruct.pfunc(1, firststruct.arguments[0], 124, 1, 1);
}
この解決策は非常に醜いものだと思います。関数ポインタを呼び出して設定するためのより良い解決策があると思います(願っています)。
十分に明確であることを願っています...注:このコードはコンパイルしませんでしたが、非常によく似たコードをコンパイルして実行したため、概念は機能しています。注:純粋なCが必要