Welcome to Duncan White's Practical Software Development (PSD) Pages.

I'm Duncan White, an experienced and professional programmer, and have been programming for well over 30 years, mainly in C and Perl, although I know many other languages. In that time, despite my best intentions:-), I just can't help learning a thing or two about the practical matters of designing, programming, testing, debugging, running projects etc. Back in 2007, I thought I'd start writing an occasional series of articles, book reviews, more general thoughts etc, all focussing on software development without all the guff.

See all my Practical Software Development (PSD) Pages

picture of D. White


Simlating OOP in ANSI C, part 3

Refresher on Inheritance

Thinking back on our Vehicle and Car example from the first part of this article, inheritance will require 4 things to work together:

It turns out that the first two (dynamic dispatch and the ability to redefine methods in a subclass) are different parts of the same thing, and better yet, we've already implemented this - our class structure contains a table of function pointers, each pointing at a method function, each class constructor can wire up those method slots to different method function implementations, and our OMn method call macros perform the runtime table lookup. In a moment we'll see a small example to prove that.

However, the last two (adding new attributes and methods and implementing subclass compatibilty) still remain to be solved, working out how to implement them this is the main goal of this part of the article.

Dynamic Dispatch and Method Redefinition without Inheritance

Before getting into full inheritance, we can demonstrate the power of dynamic dispatch (and method redefinition) by a very simple new test program that modifies an object's method table on the fly, let's call it overrideprint.c:

The Simplest form of Inheritance: Method Redefinition

From Vectors to Vehicles

More Complex Inheritance: New attributes and/or methods

How did that work: step by step

A slight modification to simpletestcar:

Summary.

In this 3-part article, I've shown how to translate single inheritance Object Oriented Programming with virtual methods into ANSI C, using the following mixture of techniques:

I can't emphasise enough that this is a safe and portable technique, not depending on any particular C compiler to work. Eric Raymond has recently written a guide called The Lost Art Of Structure Packing which describes how C compilers (portably) lay out fields in structures, how they are not allowed to reorder fields, all about padding etc. (Thanks to Latency McLaughlin of LinkedIn for this URL).

If you're still not convinced that every ANSI C compiler must abide by the structural layout rules I described, then I challenge you, gentle reader, to find me an ANSI C compiler anywhere in the world where structures are not laid out in that fashion. Please email me if you find one! Just in case I'm wrong - there might even be a free beer in it for the first person to find one:-)

If this is useful to you, I'd be delighted if you'd drop me a quick email to let me know:-) -- Duncan White, 10th Aug 2014.


d.white@imperial.ac.uk
Back to the first part Back to the second part Back to PSD Top
Written: July-August 2014