Object Oriented C

This is a discussion on Object Oriented C within the C Programming forums, part of the General Programming Boards category; Hi, I was just looking for some thoughts from all of you about object oriented programming in C. I have ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    33

    Object Oriented C

    Hi, I was just looking for some thoughts from all of you about object oriented programming in C. I have been reading the book Object Oriented Programming with ANSI by Axel-Tobias Schreiner which I'm really enjoying but I get the feeling that implementing OO in C is more work than it's worth. For example in the second chapter, he implements a simple String class that holds a pointer to the overall base class "Class" and a pointer to a char data type:

    Code:
    struct String {
        const void * class; /* must be first */
        char * text;
    };
    The class variable will point to a struct Class type:

    Code:
    struct Class {
        size_t size; // size of class e.g holds size of string class
        void * (* ctor) (void * self, va_list * app); // constructor
        void * (* dtor) (void * self); // destructor
        void * (* clone) (const void * self); // clone function
        int (* differ) (const void * self, const void * b);
    };
    To set the String class up with the necessary constructors, destructors, etc, he creates an instance of the struct Class type, the instance has functions created for the string type and they are used to assign the function pointers in the struct Class.

    Code:
    static const struct Class _String = {
        sizeof(struct String),
        String_ctor, String_dtor,
        String_clone, String_differ
    };
    The address of this is passed into a "new" function that allocates the memory for the class and calls the constructor. With this approach he also created another function called differ that was used as a polymorphic function which basically calls the specific classes differ function, in the case of the String class, it would call String_differ.

    I guess my problem with this, is the fact that there seems to me that there is a lot of unnecessary functions and pointers introduced by this approach. I'm wondering if this approach creates more overhead compared to creating the functions necessary for each class and just passing in the class specific to those functions. In essence that is what Axel-Tobias Schreiner is doing anyway, expect there is no creation of function pointers and it seems there will be less overhead as the "global" differ function used with all objects won't need to be used either.

    I know this is just an example of OOP in C, but it does seem a bit of a waste of time to create all these function pointers and polymorphic functions, just so you can access the functions like methods in a "real" class. Just wondering what you all think about this?

    Also, is this how classes were first implemented in C++ ( when it was known as C with Classes )?
    Last edited by CSaw; 07-19-2010 at 08:51 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Yes, it would create more overhead because the compiler cannot optimize function pointers, but it can optimize "normal" function calls.
    An example if the qsort function. If you compare qsort in C and C++, you may find that the C++ is significantly faster with a callback function because C++'s method of using function objects instead of function pointers allows the compiler to see--and inline--all the code.
    Myself, I see OOP in C as silly. C wasn't designed for it, and if you are using C on a platform where other OOP languages are supported, you may as well use them instead of trying to wade upriver.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    I use a fairly object oriented method when writing applications. I group the data for an object into a structure and put that structure definition in the same file as all the functions that operate on that data. This way you don't have to go through the immense pain of assigning function pointers at runtime. It's not very strict or enforcing but it gives my applications good organisation and flexibility which is what I found was the largest benefit of C++ over C (in my very short use of C++).

    This is also the way that much of the Windows API is written (and is where I took the idea from).

    I wouldn't be surprised if this was a well known method of doing things, I'm just sharing it because I only learnt about it from using the Windows API.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    33
    Thanks! Yeah I watched some Stanford University Youtube videos on creating a generic stack type, the method used was the same one you are describing here. In my opinion, I think it is some much easier to do it that way, the function pointers serve only to hold the function you had to create for the type anyway. Waste of time and resources if you ask me.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    To Deadplanet.

    Yes, that's true! I recently found a article entitled 'Object-oriented programming in C' by Paul Field. What he was introducing and using is just what you said in your thread. I'm not familiar with OOP, but the programming practice given in that article should be advocated.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Xinwu!

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    A Solution

    I have posted a practical solution to the problems of object oriented programming in C. There are some issues to be aware of when one uses C to write OO code. First, it is an implementation, not a full blown OO management system. It is a translation, not the translator. In other words, when C++ was first written as a translator to C, The management functions were part of the translator, not the translation.

    Management functions are things like being able to designate whether data are pubic or private. Actually including member functions right in the structure with the data is a management activity also. The translator kept track of these things and enforced the rules. The translation in C just executed the algorithms since all of the management details were made sure of and finished by that time.

    When a person writes code in C in an OO fashion, that person is the translator(!) not the translation written in C. Trying to implement management functions in the translated code just wastes computer power, as those functions should already have been taken care of by the translator, the programmer in this case.

    Okay, that said, I have put a complete example of programming in C on my blog at wordpress.com. They gave me some free space over there, and it gives me a little more room for the explanation of what is going on. You will have to turn some pages to get at everything. They can be accessed at any time from the right hand column. The implementation is not hard to do, and I am always available here for comment or questions. You can also comment over there.

    The coding method is fast and powerful. I have used it to implement many of the OO design patterns such as "Component Decorator, Factory, Observer, etc." I haven't included all of the instructions on how to do multiple inheritance yet, but I have the methods for that all figured out and have been having a terrific time discovering what is needed, and how to keep it fast and efficient. Right now, it only shows how to do single inheritance, and use interfaces in place of multiple inheritance, much like Java or C#. I will post my other discoveries soon.

    Copy the code, watch it work, have some fun. If you are using an older C than I am, you can still make the code work by declaring all of the "m" objects at the beginning. Since the array m[ ] is declared within braces, it will work right where it is at. Here is my blog:
    Phlsphr's Blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object Oriented Programming
    By obaid in forum C++ Programming
    Replies: 5
    Last Post: 08-22-2007, 05:31 AM
  2. object oriented paridigm??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 12-09-2005, 07:34 PM
  3. Object Oriented GUI Windows Programming?
    By silk.odyssey in forum C++ Programming
    Replies: 11
    Last Post: 05-01-2004, 08:42 PM
  4. Object Oriented - Funny story
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 09-26-2002, 02:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21