Object Construction for Appmethod C++ Libraries

From Appmethod Topics
Jump to: navigation, search

Go Up to Language Support for the Appmethod Libraries (C++)


Appmethod C++ and Object Pascal construct objects differently. This section is an overview of this topic and a description of how Appmethod C++ 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.

Object Pascal object construction

In Object Pascal, 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 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.

Appmethod C++ object construction

Object Pascal style objects are constructed like any Object Pascal 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-RTL, non-FireMonkey base classes and the first immediate RTL-FireMonkey ancestor. This RTL-FireMonkey base class is the first class to be constructed. It constructs its own base class, optionally, using inherited, following the Object Pascal method. Therefore, the 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 Object Pascal-based.

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

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 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++, Object Pascal, and Appmethod C++ are summarized in the following table:


Object Model Comparison

C++ Object Pascal Appmethod C++
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 RTL-FireMonkey base class, then construction follows the Object Pascal 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 RTL-FireMonkey base class constructor. Then according to the Object Pascal 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