Home > Articles > Programming > C/C++

  • Print
  • + Share This
Like this article? We recommend

The Object Model

The term object oriented was coined by Alan Kay in the 1960s. He went on to develop the Smalltalk language with a team at Xerox PARC as an embodiment of the ideas in this programming model.

The basic idea behind object-oriented programming is to have simple models of computers that communicate by exchanging messages. This is very different from the C++ approach, which has something a bit like object-orientation, but based on the idea of associating functions with data structures.

In C++, objects are structures with a set of associated functions that are either static or virtual. A static function is semantically equivalent to a C function with a hidden first argument containing the object. This is a completely pointless extension to C, since the following two are equivalent:

// C++
Object->function();

// C
function(Object);

The C++ version is more to type, but doesn't give any semantic differences.

C++ also supports something slightly more clever in the form of virtual member functions. If function in the previous example is a virtual function, which real function is called depends on the class of Object. If it's static, which function is called depends on what the caller thinks the class of Object is.

In Objective-C, no equivalent of a static member function was added. If you want this functionality, you just use C functions. Methods in Objective-C are similar to C++ virtual member functions, but with a few important differences. The first is that the syntax is different:

[object message];

This sends the message named message to the object. Which code is executed as a result of this action depends entirely on the class of the object. There is one very important difference between this and the C++ equivalent: In C++, this mechanism still depends slightly on what you think the class of the object is.

Consider that you have a class hierarchy with four classes. A is a root class, B and C are subclasses of A, and D is a subclass of B. If all of these except A implement a virtual doSomething() function in C++, you can only call this using a template. Consider the following line:

Object.doSomething();

If you think Object is of class B or D, and it really is D, the implementation declared by D is called. If you think it's of class C, it will call the implementation in C. If you think it's an instance of A, you need to try two explicit dynamic casts and see which one works, or use a template.

If you have the same class arrangement in Objective-C, with B, C, and D implementing a doSomething method, you could try this:

[object doSomething];

If you think object is of type B, but it's really type C, the doSomething method will still be called. If you think it's an instance of class A, you'll get a compile-time warning that instances of A don't respond to a doSomething message. If it's really an instance of B, C, or D, it will just work at runtime, but if it's really an instance of A, you'll get a runtime exception here.

Multiple inheritance is not available in Objective-C, but is far less needed than in C++. In C++, method lookup is based on the inheritance chain, so you can't use two classes interchangeably unless they have a common superclass in C++. In Objective-C, you can use any two classes entirely interchangeably if they respond to the same messages.

  • + Share This
  • 🔖 Save To Your Account