C++ Language Counterparts to the Delphi Language

From RAD Studio
Jump to: navigation, search

Go Up to Support for Delphi Data Types and Language Concepts


Delphi var and untyped parameters are not native to C++. Both have C++ language counterparts that are used in C++Builder.

Var parameters

Both C++ and Delphi have the concept of “pass by reference”. These are modifiable arguments. In Delphi, they are called var parameters. The syntax for functions that take a var parameter is:

 procedure  myFunc( var  x : Integer);

In C++, you should pass these types of parameters by reference:

 void  myFunc( int & x);

Both C++ references and pointers can be used to modify the object. However, a reference is a closer match to a var parameter because, unlike a pointer, a reference cannot be rebound and a var parameter cannot be reassigned; although, either can change the value of what it references.

Untyped parameters

Delphi allows parameters of an unspecified type. These parameters are passed to functions with no type defined. The receiving function must cast the parameter to a known type before using it. C++Builder interprets untyped parameters as pointers-to-void ( void *). The receiving function must cast the void pointer to a pointer of the desired type. Following is an example:

 int  myfunc( void * MyName) 
 
 { 
 // Cast the pointer to the correct type; then dereference it . 
 int * pi =  static_cast < int *>(MyName) ; 
 return  1 + *pi ; 
 
 }