Special member functions (destructor) - C++11, 19 of n

本文深入探讨了析构函数的作用、特性及其定义规则,包括如何正确销毁对象、理解常量、可变性和constvolatile对象的析构过程,以及默认和非默认析构函数的区分。此外,还解释了析构函数是否为虚函数的重要性,并强调了在构造和析构过程中避免使用某些特定功能的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

destructor
  1. 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.
  2. 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.
  3. 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.
  4. If a class has a base class with a virtual destructor, its destructor (whether user- or implicitly-declared) is virtual.
  5. destructor is implicitly invoked for constructed objects with thread storage duration at thread exit
  6. 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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值