[fpc-devel] Using case statement instead of VTable

Hairy Pixels genericptr at gmail.com
Tue Apr 11 10:41:20 CEST 2023



> On Apr 11, 2023, at 2:58 PM, Marco van de Voort via fpc-devel <fpc-devel at lists.freepascal.org> wrote:
> 
>>  case animal.type of
>>    TDog: TDog(animal).DoSomething;
>>    TCat: TCat(animal).DoSomething;
>>    TMouse: TMouse(animal).DoSomething;
>>  end;
> 
> This doesn't happen. There is no class that is TDog,Cat and mouse. Usually a VMT governs the relation between parent and child, i.e.   TDog and TAnimal.  The TDog nor the TAnimal implementation has no knowledge of the other mammals.

Address this part first since maybe my example was poor. I was trying to model how polymorphism is done in pure procedural code. I used to write code like this so I kind of remember.

The idea is that there is a record with an enum which is the type of the object. Then anytime you have a polymorphic function (an override) you dispatch on it using a case.

Does that make it clearer? I thought the base class TAnimal would have this “type” enum set at construction  and it would be available at runtime.

Regards,
Ryan Joseph



More information about the fpc-devel mailing list