Object-orientation in C

前端 未结 22 1710
孤城傲影
孤城傲影 2020-11-22 15:26

What would be a set of nifty preprocessor hacks (ANSI C89/ISO C90 compatible) which enable some kind of ugly (but usable) object-orientation in C?

I am familiar with

22条回答
  •  旧时难觅i
    2020-11-22 16:22

    My recommendation: keep it simple. One of the biggest issues I have is maintaining older software (sometimes over 10 years old). If the code is not simple, it can be difficult. Yes, one can write very useful OOP with polymorphism in C, but it can be difficult to read.

    I prefer simple objects that encapsulate some well-defined functionality. A great example of this is GLIB2, for example a hash table:

    GHastTable* my_hash = g_hash_table_new(g_str_hash, g_str_equal);
    int size = g_hash_table_size(my_hash);
    ...
    
    g_hash_table_remove(my_hash, some_key);
    

    The keys are:

    1. Simple architecture and design pattern
    2. Achieves basic OOP encapsulation.
    3. Easy to implement, read, understand, and maintain

提交回复
热议问题