Skip to Content
Delphi in a Nutshell
book

Delphi in a Nutshell

by Ray Lischner
March 2000
Intermediate to advanced
576 pages
18h 13m
English
O'Reilly Media, Inc.
Content preview from Delphi in a Nutshell

Name

TClass Type

Syntax

type TClass = class of TObject;

Description

TClass is the root metaclass type. You can use TClass any time you need a generic class reference.

Tips and Tricks

  • You can call any class method by invoking the method from a class reference.

  • You cannot use the is and as operators on a class reference, but you can call the InheritsFrom class method to test whether a class reference inherits from another class.

  • Delphi represents a class reference as a pointer to the class’s VMT. Thus, the size of a TClass reference is the same size as a Pointer.

Example

// Map class names to class references.
type
  TClassMap = class
  private
    fList: TStrings;
    function GetClass(const Name: string): TClass;
    procedure PutClass(const Name: string; Value: TClass);
  public
    procedure Add(ClsRef: TClass);
    property Classes[const Name: string]: TClass
        read GetClass write SetClass; default; end; procedure TClassMap.Add(ClsRef: TClass); begin // TStrings takes TObject as the associated data, // so cast TClass to TObject. fList.AddObject(ClsRef.ClassName, TObject(ClsRef)); end; function TClassMap.GetClass(const Name: string): TClass; begin Result := TClass(fList.Objects[fList.IndexOf(Name)]); end; procedure TClassMap.SetClass(const Name: string; Value: TClass); var Index: Integer; begin Index := fList.IndexOf(Name); if Index < 0 then fList.AddObject(Name, TObject(Value)) else fList.Objects[Index] := TObject(Value); end; ... var Map: TClassMap; Cls: TClass; ... Map.Add(TObject); Map.Add(TComponent); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Delphi High Performance

Delphi High Performance

Primož Gabrijelčič
Delphi Cookbook

Delphi Cookbook

Daniele Teti

Publisher Resources

ISBN: 1565926595Errata Page