[C ++/CLI] declare reference type on Stack

Source: Internet
Author: User
Tags mscorlib

In the last article about releasing resources, I mentioned that C ++/CLI provides a new Object Instantiation syntax, it is like defining an object on the static stack in native C ++. Let's take a look at this feature today. In addition, I made a mistake in answering ninputer's question in the previous post. I will correct it here...

Define the following class.

Ref   Class Test
{
Test (string ^ S)
  {
This->_ S=S;

}
  Void Hello ()
  {
Console: writeline (_ s );
}

Property string ^ Message
  {
String^ Get(){Return_ S ;}
}
 
 
Private :
String ^ _ S;
} ;

Then we define a function to use this class:

Void Foo ()
{< br> test T ( " Hello " ); // definition method when no parameter constructor exists
T. hello (); // use" dot "to access members, instead of"-> "symbol
Console: writeline (T. message); // access attributes
}

Let's take a look at the generated IlCode
. Method Assembly static void Foo () cel managed
{
// Code size 29 (0x1d)
. Maxstack 1
. Locals Init ([0] class test V_0)
Il_0000: ldstr "hello"
Il_0005: newobj instance void test:. ctor (string)
Il_000a: stloc.0
Il_000b: ldloc.0
Il_000c: Call instance void test: Hello ()
Il_0011: ldloc.0
Il_0012: Call instance string test: get_message ()
Il_0017: Call void [mscorlib] system. Console: writeline (string)
Il_001c: Ret
} // End of method 'Global functions': foo

If we change the above Foo () function to gcnew:

Void Foo ()
{
Test^T=Gcnew test ("Hello");
T->Hello ();
Console: writeline (T->Message );
}

Then observe the generated il code:

. method Assembly static void Foo () cel managed
{< br> // code size 29 (0x1d)
. maxstack 1
. locals Init ([0] class test V_0)
il_0000: ldstr "hello"
il_0005: newobj instance void test ::. ctor (string)
il_000a: stloc.0
il_000b: ldloc.0
il_000c: Call instance void test: Hello ()
il_0011: ldloc.0
il_0012: Call instance string test: get_message ()
il_0017: Call void [mscorlib] system. console: writeline (string)
il_001c: Ret
}// end of method 'Global functions': Foo

The generated il code is exactly the same as the previous one. It can be seen that this new syntax is only a mechanism provided by the compiler and does not actually instantiate objects on the stack. So why do we need to provide this syntax? Or where are the two differences? That is, explicitly releasing resources as mentioned in the previous article. Only when the stack method is used to define variables can we get the benefit of automatically calling the dispose () method.

Maybe we should try to use gcnew as little as possible in the future, unless you want to manually control the time when the dispose () method is called. The only pity is that this syntax is supported since VC 2005 tool refreshes, so the intelliisense function in VC express IDE is not keeping up with it. When the dot operator is used, there are no prompts, but I believe it will be fully supported in beta2. In addition, Ms employees also revealed that STL. NET may appear in beta2, which is very promising.

In addition:

Yesterday ninputer asked me about the stack-defined objects, whether to use-> access members or "points" to access members. The answer is "->". It should be "point. I did not know why the compiler did not report an error when I tried it a few days ago ...... unfortunately, the code is no longer there, and you cannot know what was wrong at the time. When I wrote this post today, I found it wrong...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.