In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use delete in C++ to prohibit default behavior", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "how to use delete in C++ to prohibit default behavior" bar!
If default (and no other options) behaviors are not required, use = delete to disable them
Reason (reason)
In some cases, you may not want to have a default behavior.
Example (sample) class Immortal {
Public:
~ Immortal () = delete; / / do not allow destruction
/ /...
}
Void use ()
{
Immortal ugh; / / error: ugh cannot be destroyed
Immortal* p = new Immortal {}
Delete p; / / error: cannot destroy * p
} Example (sample)
Exclusive pointers can be moved, but cannot be copied. To achieve this, the code forbids copy operations. The way to prohibit copying is to declare the copy operation originating from the left value as = delete.
Template class unique_ptr {
Public:
/ /...
Constexpr unique_ptr () noexcept
Explicit unique_ptr (pointer p) noexcept
/ /...
Unique_ptr (unique_ptr&& u) noexcept; / / move constructor
/ /...
Unique_ptr (const unique_ptr&) = delete; / / disable copy from lvalue
/ /...
}
Unique_ptr make (); / / make "something" and return it by moving
Void f ()
{
Unique_ptr pi {}
Auto pi2 {pi}; / / error: no move constructor from lvalue
Auto pi3 {make ()}; / / OK, move: the result of make () is an rvalue
}
Note: forbidden functions should be public
By convention, the deleted function (deleted functions) is declared public, not private. When user code attempts to call a member function, C++ checks its accessibility (accessibility, is it public?) before checking its delete status bit. . When a user tries to call a delete function declared as private, some compilers complain that the deleted function is declared as private
Enforcement (implementation recommendations)
Eliminating the default action should be based on the expected semantics of the class. Doubt these classes, but at the same time maintain a "positive list" of classes whose content is determined to be the right thing.
Thank you for your reading, the above is the content of "how to use delete to prohibit default behavior in C++". After the study of this article, I believe you have a deeper understanding of the problem of how to use delete to prohibit default behavior in C++, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.