Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the relationship between C++ and C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of the relationship between C++ and C language, the content is detailed and easy to understand, easy to operate, and has a certain reference value. I believe you will gain something after reading this article on the relationship between C++ and C language. let's take a look.

Error 1: there is no clear end method

It's almost certain that for most C++ programmers, the difference between C # and C # is fragment collection. This also means that programmers no longer have to worry about memory leaks and make sure 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 non-governable resources are used, they must be explicitly released after they are not used. Implicit control over resources is provided by the Finalize method (also known as finalizer), and when an object is destroyed, it is called by the fragment collector to retrieve the resources occupied by the object. Finalizer should release only the non-governable resources occupied by the destroyed object, and should not involve other objects.

Detailed introduction of C++ programming examples

This paper expounds several important elements of C++ 's programming tools.

In-depth analysis of the problems of C++ development tools

Talking about the programming skills and skills of C++ programming

A brief Analysis of C++ programming Learning and experiment system

If only governance resources are used in the program, then the Finalize method does not need and should not be executed, as long as the Finalize method is used in the processing of non-governance resources. Because finalizer takes up a certain amount of resources, you should execute finalizer. Exe only in methods that need it. Calling an object's Finalize method directly is absolutely not allowed (unless the underlying class's Finalize.) is called in the subclass's Finalize, and the fragment collector will actively call Finalize.

Syntactically, the destructor in C# is very similar to C++, but in fact they are completely different. Destructor in C++ syntax is just a shortcut to defining Finalize methods. Therefore, there is a difference between the following two pieces of code:

~ MyClass () {/ / tasks to be completed} MyClass.Finalize () {/ / tasks to be completed base.Finalize ();}

Who does error 2:Finalize and Dispose use?

We have made it clear from the above discussion that explicit calls to finalizer are not allowed and can only be called by the fragment collector. If you expect to release a limited number of non-governable resources (such as file handles) that are no longer used as soon as possible, you should use the IDisposable interface, which has a Dispose method that can help you accomplish this task. Dispose is a way to release non-governable resources without waiting for Finalize to be called.

If you have already used the Dispose method, you should prevent the fragment collector from executing the Finalize method on the appropriate object. To do this, you need to call the static method GC.SuppressFinalize and pass it a pointer to the corresponding object as a parameter, and the Finalize method can call the Dispose method. Based on this, we can get the following code:

Public void Dispose () {/ / complete cleanup operation / / tell GC not to call the Finalize method 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 than Dispose for file objects). You can call the Close method on some objects by creating a Dispose method of the private property and the Close method of the public property, and having Close call Dispose.

Since it is uncertain that Dispose will be called and the execution of finalizer is uncertain (we have no control over when GC will run), C # provides a Using statement to ensure that the Dispose method will be called as early as possible. The general method is to define which objects to use, and then use parentheses to specify an active scope for these objects. When the innermost parentheses are encountered, the Dispose method is actively called to process the object.

For some objects, it may be more appropriate to call the Close method (for example, for file objects, it is more appropriate to call Close than Dispose), you can call the C++ syntax method on some objects by creating a Dispose method of the private property and the Close method of the public property, and having Close call Dispose.

This is the end of the article on the relationship between C++ and C language. Thank you for your reading. I believe you all have a certain understanding of the knowledge of "what is the relationship between C++ and C language". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report