Field properties

Top  Previous  Next

What is translated > Properties > Field properties

The following example is taken from the Embarcadero documentation:

 

type

   THeading = 0..359;

   TCompass = class(TControl)

     private

        FHeading: THeading;

        procedure SetHeading(Value: THeading);

     published

        Property Heading: THeading read FHeading write SetHeading;

       // ...

    end;

 

 

 

C++Builder

 

 

For C++Builder  the"__property" key word is a counterpart to the Delphi properties. With that the code snippet above becomes to:

 

 

typedef int /*0..359*/ THeading;

 

class TCompass : public TControl

{

  typedef TControl inherited;

  

private:

  THeading FHeading;

  void __fastcall SetHeading(THeading Value);

__published:

  __property THeading Heading = { read = FHeading, write = SetHeading };

       // ...

};

 

 

Visual C++

 

 

Visual C++ also has compiler specific properties:

 

__declspec( property( get=get_func_name, put=put_func_name ) ) declarator

 

While in Delphi and for C++Builder the field can be set simply after the read or write specifier, Visual C++ needs functions for for the corresponding get and put specifiers. If in the original Delphi code a field is used, DelphiXE2C++11 creates an according function for it, as described for other compilers below. Such functions also are created, if the original access function is private - as in the example - or protected or if the type of the property is an array and for indexed properties.

 

class TCompass : public TControl

{

  typedef TControl inherited;

  

private:

  THeading FHeading;

  void SetHeading(THeading Value);

public:

  /*property Heading : THeading read FHeading write SetHeading;*/

  THeading ReadPropertyHeading() { return FHeading;}

  void WritePropertyHeading(THeading Value){SetHeading(Value);}

  __declspec(property(get = ReadPropertyHeading, put = WritePropertyHeading)) THeading Heading;

       // ...

};

 

In Visual C++ base class properties can be used in derived classes too, which in Delphi has to be declared explicitly.

 

 

Other compilers

 

 

For other compilers.properties are eliminated. The read and write specifications are replaced by two functions whose names are derived from the name of the original property. As default the expression "ReadProperty" or "WriteProperty" is put in front of this name respectively. You can change these prefixes in the option dialog.

 

 

class TCompass : public TControl

{

  typedef TControl inherited;

  

private:

  THeading FHeading;

  void SetHeading(THeading Value);

public:

  /*property Heading : THeading read FHeading write SetHeading;*/

  THeading ReadPropertyHeading() { return FHeading;}

  void WritePropertyHeading(THeading Value){SetHeading(Value);}

       // ...

};

 

 

The fields or methods, which originally were set in the property are now accessed via these functions. While the visibility of these fields or methods usually is private or protected, the access functions which are created by Delphi2Cpp II are public. In the "ReadProperty" function the originally field is returned or a call of the original return function is carried out. In the "WriteProperty" function the assignment to the original field is carried out and the parameters are passed to the originally method.

 

At all places in the remaining code where a property was read, the "ReadProperty" function is used and the "WriteProperty" function is called in all places, where originally a value was assigned to a property.

 

 

  if Compass.Heading = 180 then GoingSouth;

    Compass.Heading := 135;

->

  if(Compass->ReadPropertyHeading() == 180)

    GoingSouth();

  Compass->WritePropertyHeading(135);

 

 

 

 



This page belongs to the DelphiXE2Cpp11 Documentation

DelphiXE2Cpp11 home  Content