This book describes how to use Visual C ++ 6.0 for common Windows programming. In addition to a C ++ compiler, Visual C ++ 6.0 requires that the structured data type is a built-in data type of C, it is also a value type.
The C # syntax is very similar to the C ++ syntax. The difficulty of the transformation from C ++ to C # lies not in the language itself, but in familiarity. . NET. NET Framework. Although the syntax changes of c # And C ++ are very small, they will hardly affect us, but some changes are enough to keep some careless C ++ programmers in mind. In this article, we will discuss the ten simplest mistakes C ++ programmers may make.
Error 1: no clear termination method
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 non-governing resources occupied by destroyed objects, and should not involve other objects.
- C ++ programming examples
- Several important elements of C ++ programming tools
- In-depth analysis of various problems with C ++ development tools
- On the programming skills and skills of C ++ Programming
- Analysis of C ++ programming learning and Experimental System
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 the C ++ syntax is only a shortcut for defining the Finalize method. Therefore, the following two pieces of code are different:
- ~ MyClass ()
- {// Task to be completed
- }
- MyClass. Finalize () {// task to be completed
- Base. Finalize ();
- }
Error 2: Who is used by Finalize and Dispose?
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:
- Public void Dispose ()
- {
- // Complete the cleanup operation
- // Notify GC not to call the Finalize method again
- GC. SuppressFinalize (this );
- }
- Public override void Finalize (){
- Dispose (); base. Finalize ();
- }
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.
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 C ++ syntax Method for some objects.
The C ++ syntax is definitely called, and the 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.