Static Properties

From RAD Studio
Jump to: navigation, search

Go Up to Classes Index

Static properties have been implemented in C++Builder 2009 to enhance compatibility with the Delphi language

Declaring Class Functions as Static

If property access functions (getters and setters) are declared as static, you can use the property directly from the class without specifying an object. For example:

class TMyClass : TComponent {
  static int x;
  static int GetX() { return x; }
  static void SetX( int i ) { x = i; }

  __property int X = { read = GetX, write = SetX }
};

TMyClass::X = 5;
int y = TMyClass::X;

Delphi also allows plain static methods as getters and setters of properties. Note that these static methods are not class methods; there is no hidden parameter. For example:

class TTest{
  static int __fastcall GetCount();
  static void __fastcall SetCount(int I);
public:
  __property int Count = {read=GetCount, write=SetCount};
};

In this case, you can then use the properties as this:

int c = TTest::Count;
TTest t;
int i = t.Count;

The C++ compiler already supports static properties that map to a member variable. For example:

class TEncoding  {
   static int Prop;
 public:
   __property int Count = { read=Prop };
};
int f(TEncoding& t) {
   return t.Count;
}

See Also