__property

From RAD Studio
Jump to: navigation, search

Go Up to C++ Keyword Extensions


Category

Keyword Extensions

Description

The __property keyword declares an attribute of a class. Properties appear to the programmer just like any other attribute (field) of a class. However, like its Delphi counterpart, C++Builder’s __property keyword adds significantly more functionality beyond just examining and changing the value of the attribute. Since property attributes completely control access to the property, there are no restrictions on how you implement the property within the class itself.

Syntax

__property type propertyName [index1Type index1 ][indexNType indexN ] = { attributes }; 

where:

  • type is an intrinsic or previously declared data type.
  • propertyName is any valid identifier.
  • indexNType is an intrinsic or previously declared data type.
  • indexN is the name of an index parameter that is passed to the property’s read and write functions.
  • attributes is a comma-separated sequence of read, write, stored, or index.

The indexN parameters in square brackets are optional. If present, they declare an array property. The index parameters are passed to the read and write methods of the array property.

The __property keyword was added to support the VCL.

The following example shows some simple property declarations:

 class PropertyExample {

private:

	int Fx, Fy;

	float Fcells[100][100];

protected:
	int readX() {
		return (Fx);
	}

	void writeX(int newFx) {
		Fx = newFx;
	}

	double computeZ() {

		// Do some computation and return a floating-point value...

		return (0.0);

	}

	float cellValue(int row, int col) {
		return (Fcells[row][col]);
	}

public:
	__property int X = {read = readX, write = writeX};
	__property int Y = {read = Fy};
	__property double Z = {read = computeZ};
	__property float Cells[int row][int col] = {read = cellValue};

};

This example shows several property declarations:

  • Property X has read-write access through the member functions readX and writeX, respectively.
  • Property Y corresponds directly to the member variable Fy, and is read-only.
  • Property Z is a read-only value that is computed; it is not stored as a data member in the class.
  • The Cells property demonstrates an array property with two indices.

The next example shows how you would access these properties in your code:

// Previous code goes here
int main(int argc, char * argv[]) {
 PropertyExample myPropertyExample ;
 myPropertyExample.X = 42; // Evaluates to: myPropertyExample.WriteX(42) ;
 int  myVal1 = myPropertyExample.Y; // Evaluates to: myVal1 = myPropertyExample.Fy ;
 double  myVal2 = myPropertyExample.Z; // Evaluates to: myVal2 = myPropertyExample.ComputeZ() ;
 float  cellVal = myPropertyExample.Cells[3][7]; // Evaluates to : cellVal = myPropertyExample.cellValue(3,7);
}

Properties have many other variations and features not shown in this example.

Properties can also:

  • Associate the same read or write method with more than one property, by using the index attribute.
  • Have default values.
  • Be stored in a form file.
  • Extend a property defined in a base class.

Compiler Support

The BCC32 and OSX compilers do not support:

  1. The default keyword on indexed properties:
     __property int Items[int I] = {read=GetItem/*, default*/};
    
     __property int Items[int I] = {write=SetItem/*, default*/};
    
     __property int Items[int I] = {read=GetItem, write=SetItem/*, default*/};
    
  2. Compound assignment, unlike Clang-enhanced C++ compilers.
    For more information, see __property: Compound and Chained Assignment.

See Also