(a)
static const MOTOR cMotor = {
  InitPort,
  SetSpeed,
  GetSpeed,
  SetDirection,
  GetDirection
};
MOTOR* Motor;


(b)
MotorData* MotorCtor(void)
{
    MotorData* d;

    // Create the data structure 
    // and initialize the contents.
    d = Allocate(sizeof(MotorData));
    d->mPort = NULL;
    d->mSpeed = 0;
    d->mDirection = DR_UNKNOWN;

    // Create the vtable by 
    // copying it to RAM.
    if (InstCount++ == 0)
    {
        CREATE_VTABLE(Motor, cMotor, MOTOR);
    }
    return d;
}

Example 2: (a) vtable instance and global class pointer declaration; (b) constructor function.

Back to Article