The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,858 other subscribers

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

9 Responses to “What’s the point of having abstract classes in Delphi? ”

  1. Jeff's avatar

    Jeff said

    “No” is not a valid answer to the question “What’s the point of having abstract classes in Delphi?”

  2. thaddy's avatar

    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's avatar

      jpluimers said

      Did you write the Delphi part of the BASSASIO library?

    • Philipp Schaefer's avatar

      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.

  3. thaddy's avatar

    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….

  4. rvelthuis's avatar

    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's avatar

      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):

      Delphi doesn’t have abstract classes as such, only abstract methods. You will get an abstract method exception raised if you call an abstract method.

      Put simply you must not call abstract methods. The compiler emits a warming if it detects you instantiating a class with abstract methods. Good practise is to ask the compiler to turn these warnings into errors.

Leave a reply to Jeff Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.