destructor
- A destructor is used to destroy objects of its class type.
The address of a destructor shall not be taken. A destructor can be invoked for a const, volatile or const volatile object.const and volatile semantics are not applied on an object under destruction.
They stop being in effect when the destructor for the most derived object starts.
- A defaulted destructor for a class X is defined as
deleted if:
- X is a union-like class that has a variant member with a non-trivial destructor
- any of the non-static data members has class type M (or array thereof) and M has a deleted destructor or a destructor that is inaccessible from the defaulted destructor
- any direct or virtual base class has a deleted destructor or a destructor that is inaccessible from the defaulted destructor
- or, for a virtual destructor, lookup of the non-array deallocation function results in an ambiguity or in a function that is deleted or inaccessible from the defaulted destructor.
- X is a union-like class that has a variant member with a non-trivial destructor
- A destructor is trivial if it is not user-provided and if:
- the destructor is not virtual,
- all of the direct base classes of its class have trivial destructors, and
- for all of the non-static data members of its class that are of class type (or array thereof), each such class has a trivial destructor.
- the destructor is not virtual,
- If a class has a base class with a virtual destructor, its destructor (whether user- or implicitly-declared) is virtual.
- destructor is implicitly invoked for constructed objects with thread storage duration at thread exit
- Note:
- During object construction (constructor), be very careful the objects referenced have been fully initialized.
- In constructor/destructor, pay close attention to calling "virtual function, dynamic_cast, typeid". They usually will NOT work as expected. So don't call these functions or operators in constructor/destructor.
本文深入探讨了析构函数的作用、特性及其定义规则,包括如何正确销毁对象、理解常量、可变性和constvolatile对象的析构过程,以及默认和非默认析构函数的区分。此外,还解释了析构函数是否为虚函数的重要性,并强调了在构造和析构过程中避免使用某些特定功能的注意事项。
4186

被折叠的 条评论
为什么被折叠?



