Friday, March 23, 2007

Object Orientism with C

C is a language to drive you insane - me :)

Well, C is indeed a wonderful programming language.
Success of C has been limited and overshadowed by the rise of Object Oriented era.
That is indeed disheartening to C fans like me. So i myself learnt C++ with no special goal in mind and found that indeed C++ is a wonderful application language. But C can still provide OO features in complex pieces of software.
A very good example is an Operating System kernel.

Some of the ways we can employ few of the OO feature in C IMO are :-

struct my_struct{
int a;
int (*my_function)(int);

The above structure is just a declaration and not a definition. Catch is kepping declaration and definition separate gives flexibility to a C programmer e.g

struct my_struct A;
A.a = 3;
A.my_function = some_random_function_i_defined;

And again in some other source file in the same namespace i do

struct my_struct _A;
_A.a = 345;
_A.my_function = another_random_function;

Above example shows, one declaration but different invocation of the methods. Indeed fantastic and divine :).

So, this means depending on the definition we can invoke the correct function using function pointer here :).
Indeed something modern *NIX kernels make good use of.

more on this later.

No comments: