Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi tstream: strange behaviour on create

I am new of Delphi. In the documentation of TStrem class, i read it is an abstract class. So i think the compiler goes in error when i try to create it with

stream := TStream.Create();

Why not?

like image 424
user3083618 Avatar asked Mar 09 '23 19:03

user3083618


1 Answers

The Delphi language doesn't really have any formal concept of abstract class.

It is true that you can define a class to be abstract:

type
  TMyClass = class abstract
  end;

But you can perfectly well instantiate this class. In fact class abstract in Delphi is a feature used only by the long abandoned Delphi .net compiler.

A more useful definition of an abstract class is one that contains abstract methods. If you attempt to instantiate such a class, then a compiler warning will be emitted. Those warnings can be promoted to errors by way of a compiler option, if you wish.

When the documentation refers to TStream as being abstract it in fact means that it is "conceptually" abstract. In fact it does not even have any abstract methods, so by my definition above it is not abstract.

I'm really not sure why TStream does not contain abstract methods. I would suggest that the GetSize, SetSize, Read, Write and Seek should really be declared abstract. I suspect that if the class were being designed today then they would be declared abstract and likely they are not for historical reasons.

Instantiating TStream is a very common mistake made by programmers less experienced in the Delphi RTL. Once the mistake has been made a couple of times, the lesson is usually learnt. Unfortunately the system provides no easy way for this mistake to be flagged up. Each and every new programmer just has to learn the hard way.

like image 197
David Heffernan Avatar answered Mar 19 '23 23:03

David Heffernan