What’s the point of having abstract classes in Delphi?
Posted by jpluimers on 2018/10/04
There was an interesting thread a while ago: [WayBack] What’s the point of having abstract classes in Delphi? – Agustin Ortu – Google+
The answer is none (the documentation warns you against it – see Constructing instance of abstract class – the compiler doesn’t), so Stefan Glienke submitted this bug: RSP-10235: No warning for .Create on class declared as TClass = class abstract
This post is a reminder to myself to see if any progress has been made by the compiler engineers.
–jeroen
Jeff said
“No” is not a valid answer to the question “What’s the point of having abstract classes in Delphi?”
jpluimers said
You are right. None it is (;
thaddy said
I mean the VMT entries start at zero for an (fully!!!) abstract class, (data starts at actual zero) so you can can overlay an external C++ class with a Delphi abstract class when used as a cast..
I used it with the ASIO audio interfaces by Steinberg.
jpluimers said
Did you write the Delphi part of the BASSASIO library?
Philipp Schaefer said
Your point is not valid. Every class instance is a pointer to a memory block. This blocks starts with a pointer to the vmt followed by the data fields. Yes, this is compatible to C++ classes. However, abstract does not change anything in this regard. In order to have a zero pointer within the vmt for a virtual method you can flag the method itself (and not the class!) as abstract.
I highly doubt that there is a sane reason to instantiate a class with abstract methods. However, at least if the class is flagged as abstract (not only the method) one should not be able to create it.
thaddy said
And there is a very obvious use (also related to the .net background at introduction):
YOU CAN CAST AN EXTERNAL STRUCTURE TO AN ABSTRACT CLASS (because an abstract vmt starts at zero, as with C++)
When you know that, it is rather obvious….
jpluimers said
Thanks for mentioning that. I never realised this, but it is very logical.
rvelthuis said
Sorry, but you may want to rewrite this post a little. If I read it, I have no idea what this “The answer is no (the documentation warns you against it – see Constructing instance of abstract class – the compiler doesn’t)” sentence means. You may want to clarify. “The answer is no”? No what?
rvelthuis said
The compiler warns if you instantiate an abstract class, e.g. “[dcc32 Warning] Project215.dpr(28): W1020 Constructing instance of ‘MyAbstract’ containing abstract method ‘MyAbstract.DoThis'”. But an abstract class is a class that actually has abstract methods, e.g. “procedure DoThis; virtual; abstract;”, not a class that is merely declared as “class abstract”. I guess that is where the confusion comes from.
The docs for “class abstract” say: “Note: Delphi allows instantiating a class declared abstract, for backward compatibility, but this feature should not be used anymore.” (http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Classes_and_Objects_(Delphi)).
Also see this SO answer by David Heffernan (https://stackoverflow.com/a/6187091/95954):