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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "what is the role of C++ default constructor". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
C.44: the default constructor is preferably simple and does not throw an exception
Reason (reason)
The default constructor sets the content to the default state without the need for actions that may cause failure, simplifying error handling and speculation about mobile operations.
Example, problematic (problem example) template
/ / elem points to space-elem element allocated using new
Class Vector0 {
Public:
Vector0 (): Vector0 {0} {}
Vector0 (int n): elem {new T [n]}, space {elem + n}, last {elem} {}
/ /...
Private:
Own elem
T * space
T * last
}
This code is neat and normal, but may fail if an empty Vector0 object is generated after an error involving memory allocation. At the same time, let the default Vector appear as {new T [0], 0,0} is a bit wasteful. For example, Vector0v [100] requires 100 memory allocations.
There seems to be something wrong with 100 memory allocations. The translator's understanding is that as long as 100 integers are allocated at a time.
Besides, new T [0] is a little strange.
Example (sample) template
/ / elem is nullptr or elem points to space-elem element allocated using new
Class Vector1 {
Public:
/ / sets the representation to {nullptr, nullptr, nullptr}; doesn't throw
Vector1 () noexcept {}
Vector1 (int n): elem {new T [n]}, space {elem + n}, last {elem} {}
/ /...
Private:
Own elem = nullptr
T * space = nullptr
T * last = nullptr
}
Using {nullptr, nullptr, nullptr} makes Vector1 {} less expensive, but in special cases, runtime checking is required. Setting Vector1 to empty after an error is detected is a trivial matter.
Enforcement (implementation recommendations)
Prompts the constructor that throws the exception.
This is the end of the content of "what is the role of C++ default constructor". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.