|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWhen Microsoft brought out the Managed Extensions to C++ with VS.NET 7, C++ programmers accepted it with mixed reactions. While most people were happy that they could continue using C++, nearly everyone was unhappy with the ugly and twisted syntax offered by Managed C++. Microsoft obviously took the feedback it got very seriously and they decided that the MC++ syntax wasn't going to be much of a success. On October 6th 2003, the ECMA announced the creation of a new task group to oversee development of a standard set of language extensions to create a binding between the ISO standard C++ programming language and Common Language Infrastructure (CLI). It was also made known that this new set of language extensions will be known as the C++/CLI standard, which will be supported by the VC++ compiler starting with the Whidbey release (VS.NET 2005). Problems with the old syntax
What C++/CLI gives us?
Hello Worldusing namespace System;
void _tmain()
{
Console::WriteLine("Hello World");
}
Well, that doesn't look a lot different from old syntax, except that now you don't need to add a reference to mscorlib.dll because the Whidbey compiler implicitly references it whenever you compile with /clr (which now defaults to /clr:newSyntax). HandlesOne major confusion in the old syntax was that we used the * punctuator with unmanaged pointers and with managed references. In C++/CLI Microsoft introduces the concept of handles. void _tmain()
{
//The ^ punctuator represents a handle
String^ str = "Hello World";
Console::WriteLine(str);
}
The ^ punctuator (pronounced as cap) represents a handle to a managed object. According to the CLI specification a handle is a managed object reference. Handles are the new-syntax equivalent of How handles differ from pointers?
Instantiating CLR objectsvoid _tmain()
{
String^ str = gcnew String("Hello World");
Object^ o1 = gcnew Object();
Console::WriteLine(str);
}
The Basically, the Declaring typesCLR types are prefixed with an adjective that describes what sort of type it is. The following are examples of type declarations in C++/CLI :-
using namespace System;
interface class IDog
{
void Bark();
};
ref class Dog : IDog
{
public:
void Bark()
{
Console::WriteLine("Bow wow wow");
}
};
void _tmain()
{
Dog^ d = gcnew Dog();
d->Bark();
}
There, the syntax is now so much more neater to look at than the old-syntax where the above code would have been strewn with double-underscored keywords like Boxing/UnboxingBoxing is implicit (yaay!) and type-safe. A bit-wise copy is performed and an void _tmain()
{
int z = 44;
Object^ o = z; //implicit boxing
int y = *reinterpret_cast<int^>(o); //unboxing
Console::WriteLine("{0} {1} {2}",o,z,y);
z = 66;
Console::WriteLine("{0} {1} {2}",o,z,y);
}
// Output
// 44 44 44
// 44 66 44
The When you box a value-type, the returned object remembers the original value type. void _tmain()
{
int z = 44;
float f = 33.567;
Object^ o1 = z;
Object^ o2 = f;
Console::WriteLine(o1->GetType());
Console::WriteLine(o2->GetType());
}
// Output
// System.Int32
// System.Single
Thus you cannot try and unbox to a different type. void _tmain()
{
int z = 44;
float f = 33.567;
Object^ o1 = z;
Object^ o2 = f;
int y = *reinterpret_cast<int^>(o2);//System.InvalidCastException
float g = *reinterpret_cast<float^>(o1);//System.InvalidCastException
}
If you do attempt to do so, you'll get a void Box2()
{
float y=45;
Object^ o1 = y;
}
gets compiled to :- .maxstack 1
.locals (float32 V_0, object V_1)
ldnull
stloc.1
ldc.r4 45.
stloc.0
ldloc.0
box [mscorlib]System.Single
stloc.1
ret
According to the MSIL docs, "The box instruction converts the ‘raw’ valueType (an unboxed value type) into an instance of type Object (of type O). This is accomplished by creating a new object and copying the data from valueType into the newly allocated object." Further reading
ConclusionAlright, so why would anyone want to use C++/CLI when they can use C#, J# and that VB thingie for writing .NET code? Here are the four reasons I gave during my talk at DevCon 2003 in Trivandrum (Dec 2003).
|
||||||||||||||||||||||