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:

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.

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 )?

07-19-2010

Elysia

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.

07-20-2010

DeadPlanet

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.

07-20-2010

CSaw

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.

10-27-2010

xinwu

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.

10-27-2010

Adak

Welcome to the forum, Xinwu!

07-06-2011

phlsphr

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