MotorData* MotorCtor(void* data, 
                       void(**v)(void)
{
  MotorData* d;
  if (data)
  {
      // This is the base class for a 
      // defined class. The caller has
      // defined both their own 
      // data structure and vtable.
      d = data;
      if (v)
      {
          *v++ = InitPort;
          *v++ = SetSpeed;
          *v++ = GetSpeed;
          *v++ = SetDirection;
          *v++ = GetDirection;
      }
  }
  else
  {
      // This is a standalone 
      // class, and both the 
      // data structure and 
      // vtable must be created here.
      d = Allocate(sizeof(MotorData));
      if (InstCount++ == 0)
      {
        CREATE_VTABLE(Motor, 
                     cMotor, MOTOR);
      }
  }

  // Initialize the class data.
  d->mPort = NULL;
  d->mSpeed = 0;
  d->mDirection = DR_UNKNOWN;

  return d;
}

Example 5: Revised Motor constructor.

Back to Article