RAD Studio |  |

Static Properties
Declaring Class Functions as Static | See Also
Collapse All

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:

Copy Code

    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:

Copy Code

    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:

Copy Code

    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:

Copy Code

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

See Also

C++ Classes

Class Methods

Class Types

Class Scope

Class Member List

Member Functions

The Keyword This

Static Members

Inline Functions

Member Scope

Nested Types

Member Access Control

Base And Derived Class Access

Virtual Base Classes

Friends Of Classes

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!