What we have in this page?
Declaring and Using Virtual Properties
The following example shows how to declare and use virtual properties.
|

Using Multidimensional Properties
Multidimensional properties let you define property accessor methods that take a non-standard number of parameters. The following is an example program.
// more on property keyword
// compile with: /clr
#include "stdafx.h"
using namespace System;
ref class X
{
double d;
public:
X() : d(0) {}
property double MultiDimProp[int, int, int]
{
double get(int, int, int) { return d; }
void set(int i, int j, int k, double dou)
{
// do something with those ints
d = dou;
}
}
property double MultiDimProp2[int]
{
double get(int) { return d; }
void set(int i, double dou)
{
// do something with those ints
d = dou;
}
}
};
int main()
{
X ^ MyX = gcnew X();
MyX->MultiDimProp[0,0,0] = 4.5;
System::Console::WriteLine("MyX->MultiDimProp[0, 0, 0]: {0}", MyX->MultiDimProp[0, 0, 0]);
return 0;
}
Output:

Overloading Property Accessor Methods
It is possible to overload indexed properties as shown in the following example.
// more on property keyword
// compile with: /clr
#include "stdafx.h"
using namespace System;
ref class X
{
double d;
public:
X() : d(0.0) {}
property double MyProp[int]
{
double get(int i) { return d; }
double get(System::String ^ i) { return 2*d; }
void set(int i, double l) { d = i * l; }
} // end MyProp definition
};
int main()
{
X ^ MyX = gcnew X();
MyX->MyProp[2] = 7.7;
System::Console::WriteLine("MyX->MyProp[1]: {0}", MyX->MyProp[1]);
System::Console::WriteLine("MyX->MyProp[\"Test string\"]: {0}", MyX->MyProp["Test string"]);
return 0;
}
Output:
-----------------------------------------------

Declaring Abstract and Sealed Properties
Although specified as valid in the ECMA C++/CLI, in the Microsoft Visual C++ 2005 compiler you cannot specify the abstract (C++) or sealed keywords on trivial properties, or on the property declaration of a non-trivial property. To declare a sealed or abstract property, you must define a non-trivial property and specify the abstract or sealed keywords on the get and set accessor functions. Consider the following program example.
// properties, abstract and sealed
// compile with: /clr
#include "stdafx.h"
using namespace System;
ref struct A abstract
{
protected:
int m_i;
public:
A() { m_i = 100; }
// define abstract property
property int Prop_1
{
virtual int get() abstract;
virtual void set(int i) abstract;
}
};
ref struct B : A
{
private:
int m_i;
public:
B() { m_i = 200; }
// implement abstract property
property int Prop_1
{
virtual int get() override { return m_i; }
virtual void set(int i) override { m_i = i; }
}
};
ref struct C
{
private:
int m_i;
public:
C() { m_i = 300; }
// define sealed property
property int Prop_2
{
virtual int get() sealed { return m_i; }
virtual void set(int i) sealed { m_i = i; };
}
};
int main()
{
B b1;
// call implementation of abstract property
System::Console::WriteLine("b1.Prop_1: {0}", b1.Prop_1);
C c1;
// call sealed property
System::Console::WriteLine("c1.Prop_2: {0}", c1.Prop_2);
return 0;
}
Output:

Quick Reference
| To | Do this |
| Create a property for a C++ class. | Use the property keyword with get and set methods. For example:
property void sWeight(int n) { ... } property int gWeight() { ... }
|
| Implement a read-only property. | Implement only the get method. |
| Implement a write-only property. | Implement only the set method. |
| Implement an indexed property. | Implement a property whose get and set methods take parameters that are used to determine which value to get or set. For example:
property void sPay[Person^, Amount^] { ... } property Amount^ gPay[Person^] { ... }
|
|
Table 2 | |