Proper OOP? How?
Posted on 1997-08-21
Sorry about the title, couldn't think of anything better!
I have an app that responds to commands with data on the serial port. I have a set of command objects that correspond to real commands. Each one has a LoadFromStream method that gets the data from the serial port decoder (it does unencrypting and checksums etc).
Each command object is descended from TCustomCommand which has an abstract virtual LoadFromStream method.
I want to read the first byte of the stream which designates the command type, then create a command object:
obj : TCustomCommand;
Com : Byte;
case Com of
1 : Obj:=TCommand1.Create;
2 : Obj:=TCommand2.Create;
If Assigned(Obj) then
Now there's the tricky bit....the last line. Does TCommand1.LoadFromStream get called? Or does the compiler link to the declared object - ie TCustomCommand.LoadFromStream?
Can I use (Obj as Obj.ClassType).LoadFromStream?
None of the above attempts acutally compile and run. Every time it tries to call an abstract method (ie TCustomCommand.LoadFromStream) or with Obj as Obj.ClassType it tries to call TObject.LoadFromStream at compile time, and fails cos TObject doesn't have the method.
This must be possible - it's one of the great niceties of OOP, isn't it?