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/02 Report--
This article mainly explains "the reason why C++ should not call virtual functions in constructors or destructors". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "what is the reason why C++ should not call virtual functions in constructors or destructors?"
Reason (reason)
So far, the function being called should only belong to the construction object itself, not some override function that may exist in the derived class. It's very hard to understand that. In the worst case, calling an unimplemented pure virtual function directly or indirectly in a constructor or destructor can lead to undefined behavior.
Example, bad (negative example) class Base {
Public:
Virtual void f () = 0; / / not implemented
Virtual void g (); / / implemented with Base version
Virtual void h (); / / implemented with Base version
Virtual ~ Base (); / / implemented with Base version
}
Class Derived: public Base {
Public:
Void g () override; / / provide Derived implementation
Void h () final; / / provide Derived implementation
Derived ()
{
/ / BAD: attempt to call an unimplemented virtual function
F ()
/ / BAD: will call Derived::g, not dispatch further virtually
G ()
/ / GOOD: explicitly state intent to call only the visible version
Derived::g ()
/ / ok, no qualification needed, h is final
H ()
}
}
Note: calling a specific qualified function is not a virtual call, even if the function is a virtual function.
See also factory functions for how to achieve the effect of a call to a derived class function without risking undefined behavior.
Refer to the factory function to learn how to achieve the effect of invoking derived class functions without the risk of causing undefined behavior.
Note (Note)
There is nothing inherently wrong with calling virtual functions from constructors and destructors. The semantics of such calls is type safe. However, experience shows that such calls are rarely needed, easily confuse maintainers, and become a source of errors when used by novices.
Calling virtual functions from constructors and destructors is not inherently wrong. The semantics of this call is secure. However, experience has shown that such calls are rarely necessary, can easily disturb maintainers, and can become a source of errors if used by beginners.
Enforcement (implementation recommendations)
Flag calls of virtual functions from constructors and destructors.
Prompts virtual function calls from constructors or destructors.
Thank you for reading, the above is the content of "C++ do not call virtual functions in constructors or destructors". After the study of this article, I believe you have a deeper understanding of the reason why C++ should not call virtual functions in constructors or destructors, and the specific use still 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.
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.