DirectXTutorial.com
The Ultimate DirectX Tutorial
Sign In
Ref Classes and Hats
Reference Classes

Classes in C++ are probably familiar to you. In recent years, Microsoft has added a new type of class to the C++ family which it calls reference classes. These special classes were designed to play nicely with other Microsoft languages such as C#, as those languages handle classes very differently than standard C++.

A reference class (or ref class) is very similar to a normal class, but it has a few small differences that set it apart.

First, let's look at a normal class and how we might use it.

class box
{
public:
    void DoSomething();
};

int main()
{
    box myBox;

    myBox.DoSomething();
}

This is simple enough. Let's take a look at the same class using "ref class" instead.

ref class box
{
public:
    void DoSomething();
};

int main()
{
    box myBox;

    myBox.DoSomething();
}

It appears to be pretty much the same. All we did was use "ref class" instead of "class". Simple.

Let's go back and look at a normal class again, but this time let's allocate the memory dynamically.

class box
{
public:
    void DoSomething();
};

int main()
{
    box* myBox = new box;
    
    myBox->DoSomething();

    delete myBox;
}

This could have been written way better, but it's syntactically correct.

With a ref class we do something similar, but...

ref class box
{
public:
    void DoSomething();
};

int main()
{
    box^ myBox = ref new box;

    myBox->DoSomething();

    //delete myBox;
}

There are three differences here.

First, instead of creating a pointer using an asterisk, we used a "hat" (^). A hat could be called a pointer to a ref class. It's a special type of pointer.

Second, we don't call delete here. We don't need to. When the pointer myBox goes out of scope, the class is automatically and completely cleaned out of memory. Convenient!

Third, we use "ref new" instead of "new".

Using Ref Classes

Public Values and Properties

If you played around with the above code, you may have discovered that you can't create a public variable inside a ref class. It gives you an error when you compile.

To use public values, you must instead use a property. Properties look like this:

ref class box 
{
private:
    int _Number;
public:
    property int Number
    {
        int get()
        {
            return _Number;
        }
        void set(int Value)
        {
            _Number = Value;
        }
    }
};

Once you've added the property, using it is the same as using a variable.

int main()
{
    box^ myBox = ref new box;
    
    myBox->Number = 5;
    int Num = myBox->Number;
}

This gives you extra power with your variables. You can have other functions occur when a variable is changed, and you can perform calculations whenever a variable is retrieved.

Sealed Ref Classes

Another limitation to ref classes is in the constructor. You cannot have a public constructor in a ref class under normal circumstances.

However, this is a common necessity, and the only way you can get a public constructor in a ref class is to seal the class.

ref class myBox sealed
{
    myBox()    // constructor
    {
    }
};

The reason behind this is technical. A sealed class cannot be inherited by another class, and this is something a public constructor internally requires in a ref class.

Summary

Ref classes are a useful and powerful tool. There is far, far more to them than what was covered here. However, this tutorial covers enough for you to be able to program basic DirectX applications without too much trouble.

Most of the ref classes you will use are already defined, and comprise the WinRT APIs that make Windows 8 style programming possible.