Using Properties in Your C++ Classes

Properties are pseudo-data members of your classes. They are accessed just like public data members but in reality they are functions within the class.

Adding properties to Garbage Collected C++ classes is accomplished by the use of the __property keyword. Listing 1.4.14 shows a simple GC class with properties.

Listing 1.4.14. props.cpp: Simple Properties in a GC Class
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;


__gc class PropClass
{
    int m_x;
    int m_y;
    int m_z;

public:
    PropClass() : m_y(19762), m_z(3860)
    {
    }
    // read / write properties for a data member __property int get_x(){ return m_x;} __property void set_x(int v){ m_x=v;} // read only properties for a data member __property ...

Get C# and the .NET Framework: The C++ Perspective now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.