Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the reason why C++ should not call virtual functions in constructors or destructors

Shulou Source: shulou.com Published: 2022-06-02 04:15:44 10月02日 Update

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!

Tags: Function cause Clearing learning content situation behavior error security function negative object that is factory suggestion idea effect article novice more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia macOS Redmi Apple