System.Classes.TComponent.Create
Delphi
constructor Create(AOwner: TComponent); virtual;
C++
__fastcall virtual TComponent(TComponent* AOwner);
Properties
Type | Visibility | Source | Unit | Parent |
---|---|---|---|---|
constructor | public | System.Classes.pas System.Classes.hpp |
System.Classes | TComponent |
Description
Allocates memory and constructs a safely initialized instance of a component.
All objects have a Create method that constructs the object. TComponent redefines Create so that, for components, Create also:
- Establishes the relationship of a component and its Owner, as indicated by the
AOwner
parameter.
- Sets the ComponentStyle property to
csInheritable
, meaning that the component can be inherited by a descendent form type.
It is not necessary to explicitly create components added in the Form Designer. These components are created automatically when the application is run, and they are destroyed when the application is closed.
For components created programmatically, that is, not created in the Form Designer, call Create and pass in an owner component as the AOwner
parameter. The owner disposes of the component when it is destroyed. If the component is not owned, then use Free when it needs to be destroyed.
Tip: When passing in
Self
as the Owner parameter, consider what Self
references. If a component creates another component in one of its methods, then Self
refers to the first component and not the component being created, which is then owned by the first component.Note: The TComponent constructor is virtual in part to allow polymorphic instantiation of class references. This is critical to the streaming system and to the Form Designer. Do not forget to use the override directive when declaring a new component's Create constructor.