admin管理员组

文章数量:1134247

This is probably somewhat dumb or even explained in the docs but I couldn't find it.

There's a pattern that seems to repeat among a lot of Delphi classes which is the creation of a TCustomSomething before the actual TSomething. I wonder if there's any special reason for that and in case I want to create a derivation of TSomething should I inherit from TCustomSomething or directly from TSomething?

I can give some examples:

  1. The TStringStream inherits from TByteStream that inherits from TMemoryStream that finally inherits TCustomMemoryStream.
  2. TRESTClient directly inherits from TCustomRESTClient.
  3. TButton inherits from TCustomButtom.

There are indeed cases I see the actual benefit from the base TCustom... class, like in TMemo and TRichEdit, but I fail to understand why TRichEdit inherits from TCustomRichEdit instead of directly from TCustomMemo.


TL;DR: What's the use of the TCustomFoo classes in Delphi? When extending an existing class should I inherit from TCustomFoo or from TFoo directly?

This is probably somewhat dumb or even explained in the docs but I couldn't find it.

There's a pattern that seems to repeat among a lot of Delphi classes which is the creation of a TCustomSomething before the actual TSomething. I wonder if there's any special reason for that and in case I want to create a derivation of TSomething should I inherit from TCustomSomething or directly from TSomething?

I can give some examples:

  1. The TStringStream inherits from TByteStream that inherits from TMemoryStream that finally inherits TCustomMemoryStream.
  2. TRESTClient directly inherits from TCustomRESTClient.
  3. TButton inherits from TCustomButtom.

There are indeed cases I see the actual benefit from the base TCustom... class, like in TMemo and TRichEdit, but I fail to understand why TRichEdit inherits from TCustomRichEdit instead of directly from TCustomMemo.


TL;DR: What's the use of the TCustomFoo classes in Delphi? When extending an existing class should I inherit from TCustomFoo or from TFoo directly?

Share Improve this question edited Jan 8 at 8:11 AmigoJack 6,0131 gold badge19 silver badges33 bronze badges asked Jan 7 at 22:43 Arthur AraujoArthur Araujo 3151 silver badge8 bronze badges 1
  • 2 The word "Custom" in the name usually means that it provides base behaviour that descendant classes need, but they can extend from. The same applies with "Base" in the name, With any class hierarchy, you derive from a class that closest fits your needs. For example, if you wanted to have the behaviour of a TButton, but there were properties that it exposes, or behaviour that it has that you don't want, you might derive from TCustomButton instead, which is why, for example, TColorButton (in FMX) derives from TCustomButton instead of TButton - it makes no sense for it to have a Cancel property. – Dave Nottage Commented Jan 7 at 23:05
Add a comment  | 

1 Answer 1

Reset to default 7

You can inherit from either, but you'll often find that the TFoo class adds no functionality to the TCustomFoo class except for making protected properties public or published, and/or making public properties into published properties.

You may want your own TAraujoFoo components to not have all those properties public/published, in which case you can inherit from TCustomFoo instead of TFoo, and just make public/published the properties that you want TAraujoFoo to have.

本文标签: inheritanceWhat39s the reason for the TCustomXXX classes in Delphi class hierarchyStack Overflow