In Managed Extensions for C++, you can implement a standard NGWS runtime property for a managed class using the __property keyword.
To client code, a property has the appearance of an ordinary data member and can be written to or read from using the same syntax as a data member. However, instead of declaring an actual data member, you declare a pair of methods that implement the property. The compiler then generates a pseudo–data member, corresponding to the property methods. This pseudo–data member can be referenced in your source code as if it were an actual data member of the containing class.
Note You cannot take the address of a pseudo–data member.
The naming convention for a NGWS runtime property has the following properties:
The following example declares a Size
property for the CMyClass
class and then manipulates the property using the declared accessor functions:
__gc class MyClass { public: __property int get_Size() { ... }; __property void set_Size(int i) { ... }; // Pseudo–data member Size generated by compiler }; void main() { CMyClass* pC = new CMyClass; pC->Size = 10; // calls set_Size int i = pC->Size; // calls get_Size }
The following usage notes apply to the __property keyword:
Some restrictions apply for managed properties:
Managed Extensions for C++ support two types of NGWS runtime properties: scalar and indexed.
A property is scalar if the get and set methods have the following characteristics:
A property is indexed if its get and set methods have the following characteristics:
Given the previous information, a pseudo–data member array is generated with the following form (assuming a property name of Dimension
):
/* T Dimension[p1, … ,pn]; */
This property provides array-like access to the pseudo-member. However, because it is implemented using method calls, any parameter type can be used to index the pseudo–data member array.
The following example demonstrates this access. First, two classes are declared (Employee
and Manager
). Second, an instance of the Manager
class (Ed
) and two instances of the Employee
class (Bob
and Gus
) are created. The Report
property (implemented by class Manager
) is then accessed for both Bob
and Gus
:
#using <mscorlib.dll> __gc class Employee { public: String* name; ... }; __gc class Manager { public: __property Employee* get_Report(String*) { ... } __property void set_Report(String*, Employee*) { ... } // Pseudo–array member /* Employee* Report[ String* ]; */ }; void main() { Manager* Ed = new Manager; Employee* Bob = new Employee(S"Bob"); Employee* Gus = new Employee(S"Gus"); // Track Ed’s reports Ed->Report[ Bob->name ] = Bob; // indexed by String* type Ed->Report[ Gus->name ] = Gus; // indexed by String* type }