Araw
Araw

Reputation: 2478

Difference between System::String^ and System::String

What is actually the difference between System::String^ ex; and System::String ex1;? I found out that the one with "^" means that it is top-level. But what does that mean?

Upvotes: 0

Views: 457

Answers (2)

Hans Passant
Hans Passant

Reputation: 941873

C++/CLI has a feature called "stack semantics". It is meant to emulate the RAII pattern in C++, the compiler automatically emits a call to the destructor of the class when you declare a local variable of a reference type without the ^ hat.

This is not appropriate for the System::String class, it doesn't have a destructor.

Since it also doesn't have any useful constructors, you'll almost always just end up with a compiler error message when you try to declare it without the hat anyway. Always use the hat.

Upvotes: 1

David Yaw
David Yaw

Reputation: 27864

System::String^ is a reference to a managed string object, System::String is a managed string object directly on the stack or inline in another class.

As mentioned in What does the caret (‘^’) mean in C++/CLI?, the ^ is a tracking reference, roughly equivalent to * for a pointer in unmanaged code. In the same way you can have unmanagedClass* foo1; and unmanagedClass foo2;, you can have System::String^ str1; and System::String str2;

When used without the ^, it follows the same rules as an unmanaged class without the *: Access methods on it with a . not a ->. Automatically cleaned up when it leaves scope (destructor in unmanaged, dispose method in managed).

One thing that does make working with managed objects without their ^ harder is that most managed objects don't define a copy constructor or the equals operator. Neither of those would be used in C# or VB, so they generally aren't implemented. Without them, it's impossible to assign a new value to a variable without a ^, so you're generally limited to constructing just a single object.

Upvotes: 2

Related Questions