In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to better carry out the C++ constructor, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Constructor is a special method in the process of development. it is mainly used to initialize the object when creating the object, that is, the object member variables assign initial values. When the C++ constructor is carried out in C++, whenever a class or structure is created, its constructor is called.
However, the fact that the program has passed the compilation check does not mean that the error no longer exists. In the big family of "errors", the status of "grammatical errors" can only be regarded as a little brother. High-level mistakes are usually hidden deep, like a cunning criminal, and it's not easy to catch him.
According to experience, many imperceptible program errors are caused by the improper initialization or removal of variables, and the initialization and cleanup work is easy to be forgotten. Stroustrup fully considered this problem when designing the C++ language and solved it well: put the initialization of the object in the C++ constructor and the cleanup in the destructor. When an object is created, the constructor is executed automatically. When the object dies, the destructor is executed automatically. Now you don't have to worry about forgetting the initialization and cleanup of objects.
The names of constructors and destructors cannot be chosen casually and must be recognized by the compiler before they can be executed automatically. The naming method of Stroustrup is simple and reasonable: let the constructor and destructor have the same name as the class, and add the prefix'~'to show the difference because the purpose of the destructor is opposite to that of the constructor.
In addition to the name, another special thing about constructors and destructors is that there is no return value type, which is different from a function that returns a value type of void. The mission of C++ constructors and destructors is very clear, just like birth and death, coming and going naked. If they have a return type, the compiler will be at a loss. To prevent complications, simply stipulate that there is no return value type. (the above allusions refer to the literature [Eekel, p55-p56])
The C++ constructor has a special initialization method called the initialization expression table (initialization table for short). The initialization table is located after the function parameter table, but before the function body {}. This means that the initialization in the table occurs before any code in the body of the function is executed.
Rules for using constructor initialization tables:
If the class has an inheritance relationship, the derived class must call the constructor of the base class in its initialization table.
For example:
Class F {public: F (int x, int y); / / Constructor private: int massix, massively; int massii, massij;}
Data members of a class can be initialized by initializing the table or assigning values in the function body. The efficiency of these two methods is not the same. Member objects of non-internal data types should be initialized in a * * manner to achieve higher efficiency.
After reading the above, do you have any further understanding of how to better implement the C++ constructor? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.