Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the meaning of `Class of ` type declaration?

While going through one of my code, I am stuck on one statement which is as below.

TMyObjectClass = class of TMyObject;

I am a bit confused, and wondering what is the meaning of this statement. As TMyObjectClass has no declaration above the statement.

and TMyObject is having declaration as below:

TMyObject = class(TObject) private //some private member declaration Public // some public variables end;

So, my question is what is the meaning of the statement TMyObjectClass = class of TMyObject;

and How TMyObjectClass works?

I am a bit new to Delphi, so please help me to get some idea about these type of declaration and there workarounds.

like image 979
A B Avatar asked Jul 07 '14 07:07

A B


People also ask

What is meant by class declaration?

The class declaration component declares the name of the class along with other attributes such as the class's superclass, and whether the class is public, final, or abstract. At minimum, the class declaration must contain the class keyword and the name of the class that you are defining.

What is declare class in TypeScript?

declare class is for when you want to describe an existing class (usually a TypeScript class, but not always) that is going to be externally present (for example, you have two . ts files that compile to two . js files and both are included via script tags in a webpage).

What is declaration in data type?

A declaration establishes the names and characteristics of data objects used in a program. A definition allocates storage for data objects, and associates an identifier with that object. When you declare or define a type, no storage is allocated.

Which is the correct class declaration?

Which of the following is a valid class declaration? Explanation: A class declaration terminates with semicolon and starts with class keyword. only option (a) follows these rules therefore class A { int x; }; is correct.


1 Answers

This is a Class Reference.

They are used to work with meta classes. The canonical example is the Delphi streaming framework which uses

TComponentClass = class of TComponent;

This allows for dynamic binding to virtual constructors. The TComponent constructor is virtual. The streaming framework needs to instantiate classes derived from TComponent. It does so something like this:

var
  ComponentClass: TComponentClass;
  Component: TComponent;
....
ComponentClass := GetComponentClassSomehowDoesntMatterHow;
Component := ComponentClass.Create(Owner);

Now, because TComponent.Create is virtual, this is bound in a polymorphic fashion. If TComponentClass is TButton, then TButton.Create is called. If TComponentClass is TPanel, then TPanel.Create is called. And so on.

The most important thing to realise is that the class that is constructed is determined only at runtime. Note that many languages lack this capability, most notably C++.

like image 156
David Heffernan Avatar answered Sep 21 '22 12:09

David Heffernan