[C ++/CLI] The Destructor is equivalent to the idisposable: dispose () method.

Source: Internet
Author: User

In a previous post, I mentioned that in C ++/CLI, the dispose () method is automatically called. But at that time, it was only from msdn.ArticleSo everything can only be discussed on paper, and many details are unclear. I have always been thinking about how VC will provide this feature. However, this feature was not supported even when vs.net 2005 beta1 was released.

Fortunately, the compiler in the latest visual c ++ 2005 tool refresh already supports this feature. Let's look at it.

First, in the latest compiler, if a class defines a destructor, the compiler automatically implements the idisposable interface for the class and maps the destructor to the dispose () method. Suppose we define the following class

The final generated IlCodeYes.

We can see that it has implemented the idisposable interface, and the content of the dispose method is the content of the destructor.

To determine the time to automatically call the dispose () method, C ++/CLI now supports a new Object Instantiation syntax.

Generally, to obtain an instance of a class, we need to write as follows:

Test ^ t = gcnew test ();

However, to get the benefit of automatically calling the dispose () method, we must instantiate the class using the syntax as follows:

Test T;

This is as if an object is instantiated on the stack in native C ++. Of course, this is just a syntax imitation. The real object is still instantiated in the heap. However, if this method is used for instantiation, the compiler is told, but when exiting the scope of the object declaration, the dispose () method of the object is expected to be automatically called.

Let's look at a function:

Observe the Il code of the function and you can find that the code that calls the dispose () method has been added to the method. To call the dispose () method when an exception is thrown, the compiler automatically adds a try block.

If the above Foo () function is written in C #, it will be like this:

Void Foo ()
{
Using (test T = new test ())
{
}
}

However, we all know that the biggest drawback of using is that only one object can be instantiated at a time. In ado.netProgramWe often need to declare several objects at a time. At this time, the using statement is powerless. The mechanism provided by C ++/CLI perfectly solves this problem. You can declare multiple objects at a time. When these objects exit the scope, they are automatically dispose.

In addition, the scope recognition method is consistent with that in native C ++. In other words, you can add a pair of braces to form a scope, not necessarily a function, just like this:

It is worth noting that although in this post, I use the word "object out of scope", this is only to judge the time when the dispose method is called, it does not indicate that the object is recycled after exiting the scope, and the collection of the object memory is still controlled by GC, as in the C # using statement.

C ++/CLI provides a perfect solution for explicitly releasing resources after absorbing the using statements in C.

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.