Default Parameters

From Appmethod Topics
Jump to: navigation, search

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


The Pascal compiler now accepts default parameters for compatibility with C++ regarding constructors. Unlike C++, Object Pascal constructors can have the same number and types of parameters, since they are uniquely named. In such cases, dummy parameters are used in the Object Pascal constructors to distinguish them when the C++ header files are generated. For example, for a class named TInCompatible, the Object Pascal 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 Object Pascal constructors are

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

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

 __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 Object Pascal compiler 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 Appmethod library classes or when using third-party components.

See Also