Object Construction for C++Builder Libraries

From RAD Studio
Jump to: navigation, search

Go Up to C++ and Delphi Class Models


C++Builder and Delphi construct objects differently. This section is an overview of this topic and a description of how C++Builder combines these two approaches.

C++ object construction

In standard C++, the order of construction is virtual base classes, followed by base classes, and finally the derived class. The C++ syntax uses the constructor initialization list to call base class constructors. The run-time type of the object is that of the class of the current constructor being called. Virtual method dispatching follows the run-time type of the object and changes accordingly during construction.

Delphi object construction

In Delphi, only the constructor for the instantiated class is guaranteed to be called; however, the memory for base classes is allocated. Constructing each immediate base class is done by calling inherited in the corresponding derived class’s constructor. By convention, classes in the VCL,RTL and FireMonkey libraries use inherited to call (non-empty) base class constructors. Be aware, however, that this is not a requirement of the language. The run-time type of the object is established immediately as that of the instantiated class and does not change as base class constructors are called. Virtual method dispatching follows the run-time type of the object and, therefore, does not change during construction.

C++Builder object construction

Delphi style objects are constructed like any Delphi objects, but using C++ syntax. This means that the method and order of calling base class constructors follows C++ syntax using the initialization list for all non-VCL, non-RTL, non-FireMonkey base classes and the first immediate VCL-RTL-FireMonkey ancestor. This VCL-RTL-FireMonkey base class is the first class to be constructed. It constructs its own base class, optionally, using inherited, following the Delphi method. Therefore, the VCL-RTL-FireMonkey base classes are constructed in the opposite order from which you might expect in C++. Then the C++ base classes are all constructed, from the most distant ancestor to the derived class. The runtime type of the object and virtual method dispatching are Delphi-based.

The following figure illustrates the construction of an instance of a Delphi style class, MyDerived, descended from MyBase, which is a direct descendant of TWinControl. MyDerived and MyBase are implemented in C++. TWinControl is a VCL class implemented in Delphi.

Order of Construction.png

Inheritance Order of construction

Note: The order of construction might seem backwards to a C++ programmer because it starts from the leaf-most ancestor to TObject for true VCL-RTL-FireMonkey classes, then constructs MyBase, and finally constructs the derived class.

Note: TComponent does not call inherited because TPersistent does not have a constructor. TObject has an empty constructor, so it is not called. If these class constructors were called, the order would follow the diagram in which these classes appear in gray.

The object construction models in C++, Delphi, and C++Builder are summarized in the following table:


Object Model Comparison

C++ Delphi C++Builder
Order of Construction

Virtual base classes, then base classes, finally the derived class.

Instantiated class constructor is the first and only constructor to be called automatically. If subsequent classes are constructed, they are constructed from leaf-most to root.

Most immediate VCL-RTL-FireMonkey base class, then construction follows the Delphi model, then construction follows the C++ model (except that no virtual base classes are allowed).

Method of Calling Base Class Constructors

Automatically, from the constructor initialization list.

Optionally, explicitly, and at any time during the body of the derived class constructor, by using the inherited keyword.

Automatically from the constructor initialization list through the most immediate ancestor that is a VCL-RTL-FireMonkey base class constructor. Then according to the Delphi method, calling constructors with inherited.

Run-time Type of the Object as It is Being Constructed

Changes, reflecting the type of the current constructor class.

Established immediately as that of the instantiated class.

Established immediately as that of the instantiated class.

Virtual Method Dispatching

Changes according to the run-time type of the object as base class constructors are called.

Follows the run-time type of the object, which remains the same throughout calls to all class constructors.

Follows the run-time type of the object, which remains the same throughout calls to all class constructors.


The section Calling Virtual Methods in Base Class Constructors describes the significance of these differences.

See Also