Object Oriented C: Methods

In the last post, we created a simple framework for initializing and finalizing objects using ansi C. Today we’ll try to add  type specific methods to that framework.

We’ll start by embedding the type information inside the classes themselves:

Each class we declare will have a pointer to its class definition as its first element. It will be initialized by the create operation and later operations (method call, destruction etc) will use this field to access the type specific information of this object. This allows us to stop manually keeping track of the objects we create. The implementation of create and destroy becomes:

Note that destroy no longer needs the type of object it’s destroying as an argument. It figures it out at runtime with the getClass function, set by  create as the first word of object it allocated to its type (line 7).

With that out of the way, let’s get into type-specific methods. Ideally, the type itself should contain all the methods which belong to the type. We can achieve that by modifying struct type like this:

mc is the number of methods, mns is an array of the names of methods, and vtbl is an array of the pointers to the methods.

methodSignature is the signature every method should have. The methods of a class should take the invocation target object as the first argument, followed by the other arguments. For example, the implementation of a method IntArray_set, which sets the value at an index of the array will become:

It interprets the arguments passed to it as two ints and sets the corresponding element of the backend array. Note that it returns a void *, so the caller has to interpret the return type as it sees fit. We’ll assign this method to IntArray class like this:

This is the implementation of call operation:

We can set a requirement upon the users to only return objects compatible with this framework from the methods of classes defined with this framework, but that isn’t necessary. However, if we choose to do that, we’ll have to provide a standard library for use with this framework, if we want it to be useable at all, and the type system will look a lot like that of python (everything will be an object).

This is how we’ll use the class IntArray created with this framework:

Works correctly and prints 12 as output. Let’s try adding a method which returns the sum of all elements in an IntArray:

We’ll test it like this:

It prints 45, as expected.

Note that there’s a stray malloc in out method. We should wrap this int in a class and return an instance of that class from the  sum method. This implementation is becoming more and more pythonic, and that might be a problem. Python has a garbage collector, hence the programmer doesn’t need to worry about object destruction. This framework doesn’t provide full backward compatibility with C types in a way similar to C++. We’ll try to solve this problem and implement a useable inheritence model in later posts. Here‘s the code used in today’s post

 See you later.

Leave a Reply

Your email address will not be published. Required fields are marked *