45

I was reading Ivor Horton's Beginning Visual C++ 2008 and many of its CLR examples have this definition for main:

int main(array<System::String ^> ^args)

I went back, page by page, to the beginning of the book to find the first such instance with an explanation what it really means, but couldn't find one.

Obviously it means the same as the standard int main(int argc, char *argv[]), but I'd like to know when and why that ^ is really used, and why it even exists (does it do something that pointers * and references & cannot represent)?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Tuminoid
  • 9,445
  • 7
  • 36
  • 51

3 Answers3

45

It's a managed pointer - while * marks a pointer to an object that is unmanaged, ^ points to a garbage collected object (handled by the framework). Read this for more information about the way pointers are handled in .NET.

Saulius Valatka
  • 7,017
  • 5
  • 26
  • 27
20

Just to add to that, in C++/CLI, managed pointers are handled separately from normal pointers, so you even allocate them with a different keyword:

NativeObject* n = new NativeObject();
ManagedObject^ m = gcnew ManagedObject();

Managed and Native objects are two completely different things and you can't mix them (well, not easily).

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
Ray Hidayat
  • 16,055
  • 4
  • 37
  • 43
0

See this for full discussion: http://msdn.microsoft.com/de-de/library/yk97tc08.aspx:

A handle to an object on the managed heap points to the "whole" object, and not to a member of the object.

ng5000
  • 12,330
  • 10
  • 51
  • 64