Constructors and Class References

A constructor can be called using a variable of a class-reference type. This allows construction of objects whose type isn't known at compile time. For example, type TControlClass = class of TControl;

function CreateControl(ControlClass: TControlClass;

const ControlName: string; X, Y, W, H: Integer): TControl;


Result := ControlClass.Create(MainForm); with Result do begin

Parent := MainForm; Name := ControlName; SetBounds(X, Y, W, H); Visible := True; end;


The CreateControl function requires a class-reference parameter to tell it what kind of control to create. It uses this parameter to call the class's constructor. Because class-type identifiers denote class-reference values, a call to CreateControl can specify the identifier of the class to create an instance of. For example,

Constructors called using class references are usually virtual. The constructor implementation activated by the call depends on the runtime type of the class reference.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment