1

I'm currently following a quick tutorial. And the XOR "^" symbol has cropped up in a place I've never seen it before. Exactly where the asterisk would be if I were declaring a pointer to an object. See it in the code snippet below.

COpenGL(System::Windows::Forms::Form ^ parentForm, 
            GLsizei iWidth, GLsizei iHeight)
    {
        CreateParams^ cp = gcnew CreateParams;

        // Set the position on the form
        cp->X = 100;
        cp->Y = 100;
        cp->Height = iWidth;
        cp->Width = iHeight;

Afterwards the object cp is dereferenced as if it were a pointer to an object of CreateParams type.

So....what does the ^ mean? Google has not been too friendly.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
Guy Joel McLean
  • 1,019
  • 4
  • 12
  • 34

2 Answers2

3

C++/CLI is a different language from C++. It is sort of an extension of C++. The key new feature is that of garbage-collected CLI objects. The type T ^ denotes a tracked pointer to such an object, which is created with the new keyword gcnew. Similarly, T % denotes a tracked reference.

In fact, there's a whole new part of the type system, with a notion of "managed classes" (designated ref class/ref struct), which have both destructors and finalizers. In fact, I was once so confused about this that I asked a question about that.

An excellent read is Herb Sutter's design rationale for C++/CLI.

Community
  • 1
  • 1
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
2

The ^ is part of a Microsoft extension to C++, namely C++/CLI or C++/CX. Microsoft introduced it to denote garbage collected pointers, to be used in .NET or WinRT. You see the gcnew call - it means that is not a "normal" memory allocation, but its garbage collected memory from the runtime.

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
  • C++/CX is a whole different animal altogether. – Kerrek SB Apr 30 '13 at 10:25
  • @KerrekSB indeed it is, but it uses the same `^` notation, although the mechanisms under the hood are somewhat different. – Arne Mertz Apr 30 '13 at 10:27
  • Thanks, so a pointer to an object, created with a call to gcnew is donated by ^. Cheers guys – Guy Joel McLean Apr 30 '13 at 10:36
  • @GuyJoelMcLean yes, but note that it's not only the creation that matters. It is in many ways not comparable to plain C pointers, since the garbage collector "tracks" the memory it points to and releases that memory if the object is no longer used. – Arne Mertz Apr 30 '13 at 11:36