I will introduce you to the C ++ programming Portal

Source: Internet
Author: User

In this article, we will discuss the ten simplest mistakes C ++ programmers make for commercial purposes. Some commercial companies may add features that they think are feasible or better. The following is a learning discussion ..

For most C ++ programmers, the biggest difference between C # And C ++ is fragment collection. This also means that programmers no longer have to worry about memory leaks and ensure that all useless pointers are deleted. But we can no longer accurately control the process of killing useless objects. In fact, there is no clear destructor in C.

If you use a non-governing rational resource, you must release it explicitly after you do not use the resource. Implicit control over resources is provided by the Finalize method, also known as finalizer. when an object is destroyed, it will be called by the fragment collection program to reclaim the resources occupied by the object. Finalizer should only release the non-governing resources occupied by the destroyed object.

  • Analysis of Visual C ++'s two complete Windows Applications
  • Explore the rich and colorful C ++ Technology
  • In-depth analysis of C ++ code compilation and statement Sequence
  • Programming in the Visual C ++ Environment
  • Senior Scholars Talk About C ++ programming skills

Other objects should not be involved. If only the rational resources are used in the program, the Finalize method is not required and should not be executed. The Finalize method is used only when the resources are not rational. Because the finalizer occupies a certain amount of resources, you should only execute finalizer in the method that requires it. directly calling the Finalize method of an object is absolutely not acceptable unless the Finalize of the basic class is called in the Finalize of the subclass .), the fragment collector will actively call Finalize.

In terms of syntax, the destructor in C # is very similar to C ++, but they are actually completely different. The destructor in C # is just a shortcut to define the Finalize method. Therefore, the following two pieces of code are different:

 
 
  1. ~ MyClass ()
  2. {// Task to be completed
  3. }
  4. MyClass. Finalize () {// task to be completed
  5. Base. Finalize ();
  6. }

From the above discussion, we have made it clear that explicit calls to finalizer are not acceptable. It can only be called by the fragment collection program. If you want to release a limited number of unmanageable resources such as file handles as soon as possible, you should use the IDisposable interface, which has a Dispose method, it can help you complete this task. Dispose is a method that can release non-governing resources without waiting for Finalize to be called.

If the Dispose method has been used, the fragment collection program should be prevented from executing the Finalize method on the corresponding object. Therefore, you need to call the Static Method GC. SuppressFinalize and pass the pointer of the corresponding object to it as a parameter. The Finalize method can call the Dispose method. Accordingly, we can get the following code:

 
 
  1. Public void Dispose ()
  2. {
  3. // Complete the cleanup operation
  4. // Notify GC not to call the Finalize method again
  5. GC. SuppressFinalize (this );
  6. }
  7. Public override void Finalize (){
  8. Dispose (); base. Finalize ();
  9. }

For some objects, it may be more appropriate to call the Close method. For example, it is more appropriate to call Close for object objects than Dispose ), you can create a Dispose method for the private attribute and a Close method for the public attribute, and call Dispose to call the Close method for some objects.

Dispose is always called, and execution of finalizer is also uncertain. We cannot control when the GC will run ), C ++ provides a Using statement to ensure that the Dispose method is called as early as possible. The general method is to define which object to use, and then use parentheses to specify an activity range for these objects. When the inner bracket is reached, the Dispose method will be called proactively, process the object.

In the first part of this example, C ++ programming is created in the Using statement. When the Using statement ends, the system calls Dispose to process the Font object. In the second part of this example, the Font object is created outside the Using statement. When you decide to use it, place it in the Using statement. When the Using statement ends, the system will call Dispose. the Using statement can also prevent other accidents and ensure that the system will call Dispose.

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.