• Advertisement
Sign in to follow this  

Caret After Variable Type in C++?

This topic is 3839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been programming for years in C++ but I've only recently started encountering this syntax where the type of a variable has a caret appended to the end of it. Here's an example from MSDN:
using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Assume the current culture is en-US.
   // The date is February 16, 1992, 12 hours, 15 minutes and 12 seconds.
   String^ myDateTimeValue = "2/16/1992 12:15:12";
   DateTime myDateTime = DateTime::Parse( myDateTimeValue );
   Console::WriteLine( "1) myDateTime       = {0}", myDateTime );
   
   // Reverse month and day to conform to a different culture.
   // The date is February 16, 1992, 12 hours, 15 minutes and 12 seconds.
   IFormatProvider^ culture = gcnew CultureInfo( "fr-FR",true );
   String^ myDateTimeFrenchValue = "    16/02/1992 12:15:12";
   DateTime myDateTimeFrench = DateTime::Parse( myDateTimeFrenchValue, culture, DateTimestyles::NoCurrentDateDefault );
   Console::WriteLine( "2) myDateTimeFrench = {0}", myDateTimeFrench );
   
   // The date is February 16, 1992, 12 hours, 15 minutes and 12 seconds.
   array<String^>^expectedFormats = {"G","g","f","F"};
   myDateTimeFrench = DateTime::ParseExact( myDateTimeFrenchValue, expectedFormats, culture, DateTimestyles::AllowWhiteSpaces );
   Console::WriteLine( "3) myDateTimeFrench = {0}", myDateTimeFrench );
}

/*
This example yields the following results:

1) myDateTime       = 2/16/1992 12:15:12 PM
2) myDateTimeFrench = 2/16/1992 12:15:12 PM
3) myDateTimeFrench = 2/16/1992 12:15:12 PM
*/
[ From http://msdn2.microsoft.com/en-us/library/1k1skd40.aspx ] What is this caret (^) sprinkled all in this code? How is a String^ any different than a String? Thank you for your help, Austin Matthews

Share this post


Link to post
Share on other sites
Advertisement
That's C++/CLI, not standard C++. It indicates a managed reference.

Share this post


Link to post
Share on other sites
Thank you for clarifying. It makes sense that it's a managed thing, since I didn't think there were any glaring holes in my knowledge of standard C++.

However, what exactly is the difference between String^ and String&? I can't imagine why String& would not be perfectly sufficient within the context of .NET and Managed C++.

Share this post


Link to post
Share on other sites
A "managed reference" is a beast which semantically is much more like a pointer than a C++ reference. What it offers over pointers is a mechanism for telling the .NET garbage collector that one object owns a reference to another object.

Share this post


Link to post
Share on other sites
A managed reference (usually called a handle in C++/CLI) is a reference to a managed .NET object. Managed object's lifetimes are controlled by the .NET garbage collection system which needs to be able to track all references to managed objects. Having separate syntax for managed references / handles and regular native references helps makes this distinction explicit in the code and avoid confusion between managed / garbage collected .NET types and unmanaged native types whose lifetimes must still be managed explicitly by the programmer.

Share this post


Link to post
Share on other sites
Sorry, I meant reference in the .NET sense of the term, not the C++ sense of the term. I believe C++/CLI terminology for it is actually a "handle". It's basically a managed pointer, and more or less equivalent to __gc pointers in Managed C++.

Share this post


Link to post
Share on other sites
Thank you all. That makes sense. Now perhaps I can more successfully read .NET code.

Share this post


Link to post
Share on other sites
Quote:
Original post by ms291052
However, what exactly is the difference between String^ and String&? I can't imagine why String& would not be perfectly sufficient within the context of .NET and Managed C++.


I think there is slight confussion with terms, top-hat notation the caret is a handle to a garabage collected heap, you can use standard C++ references because the garabage collector can move objects around in memory and thus making them invalid. However there does exist C++/CLI tracked reference type which is the %. Again to help clarify:


T* -> T^
T& -> T%

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement