C言語でのオブジェクト指向を可能とするCos
The C Object System: Using C as a High-Level Object-Oriented Language
http://arxiv.org/abs/1003.2547v1
C言語をオブジェクト指向っぽく使えるライブラリである.論文中にはオブジェクト指向言語との対比としてObjective-Cを挙げている.
C Object Systemを使ったコードと,相当するObjective-Cのコードは以下のようになる.
クラス利用の場合
// C言語 #include <cos/Object.h> #include <cos/generics.h> useclass(Counter, (Stdout)out); int main(void) { OBJ cnt = gnew(Counter); gput(out,cnt); gdelete(cnt); }
// Objective-C #include <objc/Object.h> // Counter interface isn’t exposed intentionally @class Counter, Stdout; int main(void) { id cnt = [Counter new]; [Stdout put: cnt]; [cnt release]; }
クラス定義の場合
// C言語 defclass(Counter) int cnt; endclass
// Objective-C @interface Counter : Object { int cnt; } // declaration of Counter methods not shown @end
この他にも継承や,例外,移譲なんてのもあるようだ.はっきり言ってキモイ.しかし,自分はObjective-Cはあまり詳しくないので,このキモさを十分に堪能出来ないのは残念だ.