Var Pointer Parameters

Top  Previous  Next

What is translated > Routines > Parameter types > Var Pointer Parameters

There is a known problem with var parameters of pointer type.

In Delphi, you can pass any pointer type to a var parameter of type Pointer, but C++ requires exact type matches for reference parameters.

 

For example:

 

void* ReallocMem(void*& P, size_t Size);

 

char* buf;

ReallocMem(buf, 10);  // Error: cannot convert 'char*' to 'void*&'

 

 

That’s why C++Builder defines only:

 

void* __cdecl ReallocMemory(void* P, NativeInt Size);

 

 

and not a version with void*&.

 

A good solution for Delphi2Cpp would be a template wrapper:

 

template <typename T>

void ReallocMem(T*& P, size_t Size)

{

    // implementation

}

 

 

However, this approach can only be used for certain RTL/VCL helper functions,

because templates require the function definition to reside in the header file.

General user-defined routines cannot be converted this way automatically.

 

Therefore, Delphi2Cpp uses a more general solution:

When translating a var pointer parameter that causes a type mismatch, Delphi2Cpp automatically inserts a typecast:

 

ReallocMem((void*&)Buf, 10);

 

untyped var parameters

 

Untyped var parameters in Delphi are translated to **void** parameters in C++.

The caller passes the address of the variable, and inside the routine the parameter is dereferenced as needed.



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content