I have a class Packet with an abstract method getID:
Type
Packet = class
...
function getID() : Integer;virtual;abstract;
class procedure writePacket(par0 : Packet; par1 : TIdTCPConnection);
...
implementation
class procedure Packet.writePacket(par0 : Packet; par1 : TIdTCPConnection);
// par1 is a TCP connection used to send data through the network;
begin
par1.writeInteger(par0.getID());
//some code following
end;
I have the following subclass:
type
PacketTest = class(Packet)
...
function getID() : Integer;
...
function PacketTest.getID():Integer;
begin
result := {some value individual for each subclass}
end;
Now I call the superclass’s class procedure writePacket with a subclass of packet as par0, which should then call the subclass’s function getID.
Instead it calls the superclass’s function getID and (of course) throws an abstract error.
I want it to dynamically call the subclass’s function getID, which should never cause a problem because par0 is always a subclass of Packet since Packet itself is abstract.
The declaration of
getIDin the subclass must have the override keyword: