OOPC (Object Oriented Programming in C) Conventions

OOPBecause C is not an object oriented programming language, if you want to use it for object oriented programming, you need to do it yourself. But how best to do this? Here are some of the conventions I prefer to follow:

  1. If the size of the class is fixed, it should be declared like this:

    typedef struct _package_Class {
    … members …
    } * package_Class;

  2. The size of a fixed sized class can be determined with a macro like:

    #define SEYMOUR_CLASS_SIZEOF(className) sizeof(struct _##className)

  3. Fixed size objects can be allocated on the stack as follows:

    package_Class object = alloca(SEYMOUR_CLASS_SIZEOF(className));

  4. If the size of the class is variable, it should be declared like this:

    typedef struct _package_Class * package_Class;

  5. The size of a variably sized class should be determinable by a function, which you need to provide:

    size_t package_Class_SizeOf(…parameters…)

  6. Objects of variably sized classes can be allocated on the stack using alloca, and dynamically using malloc. For example, this object will be allocated on the stack and automatically deallocated on return from the function:

    package_Class object = alloca(package_Class_SizeOf(1024)));

  7. Preallocated objects should be constructed using:

    void package_Class_construct(package_Class this, …parameters…)

  8. If there are special constraints on the allocation and deallocation of objects of a class, the class may omit the constructor and implement only an allocator:

    package_Class package_Class_New(…parameters…)

  9. In this case, the corresponding deallocator should be used to free the object:

    void package_Class_delete(package_Class this)

  10. Classes should implement a toString method:

    const char * package_Class_toString(
    package_Class this, char * bufferPointer, size_t bufferSize)

Following a consistent convention around the meaning of object variables (in this convention, objects are always passed by reference, and you never need to take their addresses) makes code easier to understand and verify.

Consistent naming conventions do the same. For example, _New and _SizeOf are upper/pascal case because they are functions (or “class methods”, if you prefer). But _construct, _delete and _toString are lower/camel case because they are object methods. The first parameter passed to a method is always an object (by convention, named this).


About jimbelton

I'm a software developer, and a writer of both fiction and non-fiction, and I blog about movies, books, and philosophy. My interest in religious philosophy and the search for the truth inspires much of my writing.
This entry was posted in c programming and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s