Default Parameters

From RAD Studio
Jump to: navigation, search

Go Up to Support for Delphi Data Types and Language Concepts


The Pascal compiler now accepts default parameters for compatibility with C++ regarding constructors. Unlike C++, Delphi constructors can have the same number and types of parameters, since they are uniquely named. In such cases, dummy parameters are used in the Delphi constructors to distinguish them when the C++ header files are generated. For example, for a class named TInCompatible, the Delphi constructors could be:

 constructor  Create(AOwner: TComponent) ; 
 constructor  CreateNew(AOwner: TComponent) ;

which would translate, without default parameters, to the following ambiguous code in C++ for both constructors:

 __fastcall  TInCompatible(Classes::TComponent* Owner);
 // C++ version of the Pascal Create constructor 
 
 __fastcall  TInCompatible(Classes::TComponent* Owner);
 // C++ version of the Pascal CreateNew constructor

However, using default parameters, for a class named TCompatible, the Delphi constructors are:

 constructor  Create(AOwner: TComponent) ; 
 constructor  CreateNew(AOwner: TComponent; Dummy: Integer = 0) ;

They translate to the following unambiguous code in C++Builder:

 __fastcall  TCompatible(Classes::TComponent* Owner);
 // C++ version of the Pascal Create constructor 
 
 __fastcall  TCompatible(Classes::TComponent* Owner, int Dummy);
 // C++ version of the Pascal CreateNew constructor

Note: The main issue regarding default parameters is that the Delphi compiler (DCC32) strips out the default value of the default parameter. Failure to remove the default value would lead to the ambiguity that would occur if there were not defaults at all. You should be aware of this when using RAD Studio library classes or when using third-party components.