[futurebasic] Re: [FB] OOP for dummies

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 1998 : Group Archive : Group : All Groups

From: Mars Saxman <marssaxman@...>
Date: Wed, 21 Oct 98 18:23:02 -0800
>In the control manager, one of the things you can do is assign the address
>of an action routine to a part in a control.  I envision OOP, at its
>simplest level, as the creation of objects which have routines assigned to
>each of their parts.  Of course, there are classes and polymorphism and all
>those words that make one want to run to assembler, but is this basic idea
>correct, or am I missing the point?

That's the simplest way of doing it, and I believe many C++ compilers do 
it that way.

Basically, a class is a fancy struct (record). When you declare a virtual 
function (one that can be called polymorphically) inside the class, the 
compiler inserts a pointer to the function's code at the appropriate 
spot. So this:

class foo {
  public:
   void gadget(void);
   int data;
};

is basically the same thing as this:

struct foo {
  void (gadget*)(void);
  int data;
};

The compiler invisibly generates code to fill out the function pointers 
whenever you create a new instance of a class.

Object Basic takes a slightly different solution, which is also used in 
Delphi (Object Pascal). The first field of every object is an invisible 
pointer to a structure called a "vtable". The vtable is an array of 
function pointers - one entry for each function.

You can write object-oriented code in any language that supports records 
and function pointers. There was a demo floating around a few years back 
that showed how to do it in FutureBASIC. But the fact that you can do it 
doesn't mean it's going to be easy, and that's why specifically 
object-oriented languages exist. There are a *lot* of neat things a 
compiler can do for you if it is allowed to.

-Mars