名前空間
変種
操作

Destructors

提供: cppreference.com
< cpp‎ | language

 
 
C + +言語
一般的なトピック
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
フロー制御
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件付き実行文
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
繰り返し文
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
文をジャンプします
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
関数の宣言
ラムダ関数の宣言
関数テンプレート
の歴史。インライン指定
例外仕様 (廃止予定)
noexcept指定子 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
名前空間
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
指定子
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV指定
貯蔵期間指定
constexprの指定子 (C++11)
自動指定 (C++11)
alignas指定子 (C++11)
初期化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
リテラル
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
代替表現
ユーティリティ
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
型の別名宣言 (C++11)
属性 (C++11)
キャストします
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
暗黙の型変換
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Cスタイルキャストと機能
メモリの割り当て
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス固有の機能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
明示 (C++11)
静的
特殊なメンバ関数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
テンプレート
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラステンプレート
関数テンプレート
テンプレートの特殊化
パラメーターパック (C++11)
その他
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
インラインアセンブリ
 
デストラクタはオブジェクトの有効期間が終了するときに呼び出される特別なメンバ関数です。デストラクタの目的はオブジェクトがその存続期間中に取得した可能性があるリソースを解放することです.
Original:
A destructor is a special member function that is called when the lifetime of an object ends. The purpose of the destructor is to free the resources that the object may have acquired during its lifetime.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集] 構文

~class_name (); (1)
virtual ~class_name (); (2)
~class_name () = default; (3) (C++11およびそれ以降)
~class_name () = delete; (4) (C++11およびそれ以降)

[編集] 説明

デストラクタ第典型宣言
Original:
# Typical declaration of a destructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#仮想デストラクタは、通常、基本クラスで必要とされる
Original:
# Virtual destructor is usually required in a base class
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#デストラクタは、コンパイラによって生成されるように強制する
Original:
# Forcing a destructor to be generated by the compiler
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#暗黙のデストラクタを無効にする
Original:
# Disabling the implicit destructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
オブジェクトの有効期間が含まれ、終了したときにデストラクタが呼び出されます
Original:
The destructor is called whenever an object's lifetime ends, which includes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • 静的記憶域期間を持つオブジェクトのプログラム終了
    Original:
    program termination, for objects with static storage duration
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • スレッドローカル記憶域期間を持つオブジェクトのため(C++11およびそれ以降)スレッド終了
    Original:
    thread exit, for objects with thread-local storage duration (C++11およびそれ以降)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 自動記憶域期間を持つオブジェクトの参照とその生活に結合することによって、拡張された一時変数のスコープの終わり
    Original:
    end of scope, for objects with automatic storage duration and for temporaries whose life was extended by binding to a reference
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 動的記憶域期間を持つオブジェクトの削除式、
    Original:
    delete-expression, for objects with dynamic storage duration
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 無名の一時のための完全な表現の終わり
    Original:
    end of the full expression, for nameless temporaries
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 例外がキャッチされ、それらのブロックをエスケープするときに、自動記憶域期間を持つオブジェクトに対して、スタックの巻き.
    Original:
    stack unwinding, for objects with automatic storage duration when an exception escapes their block, uncaught.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
デストラクタはまた、例えば、直接呼び出すことができますアロケータ経由で構築されたオブジェクトを破棄するために、プレースメントや、新しいのstd ::アロケータ::破壊する()としてアロケータのメンバ関数を介して使用して構築されたオブジェクトを破棄します。そのようなローカル変数として、通常のオブジェクトに直接デストラクタを呼び出すことに注意してください、デストラクタがスコープの最後で、再び呼び出されたときに未定義の動作を呼び出す.
Original:
The destructor may also be called directly, e.g. to destroy an object that was constructed using placement-new or through an allocator member function such as のstd ::アロケータ::破壊する(), to destroy an object that was constructed through the allocator. Note that calling a destructor directly for an ordinary object, such as a local variable, invokes undefined behavior when the destructor is called again, at the end of scope.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] デストラクタは暗黙的に宣言された

は、ユーザー定義のデストラクタがクラス型(structclass、またはunion)のために提供されていない場合、コンパイラは常にそのクラスのinline publicメンバーとしてデストラクタを宣言します.
Original:
If no user-defined destructor is provided for a class type (struct, class, or union), the compiler will always declare a destructor as an inline public member of its class.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 削除されたコピーのデストラクタを暗黙的に宣言された

クラスTための暗黙的に宣言された値またはデフォルトデストラクタは未定義(C++11以前)/削除(C++11およびそれ以降)次のいずれかに該当する場合のように定義されます
Original:
The implicitly-declared or defaulted destructor for class T is undefined (C++11以前) / defined as deleted (C++11およびそれ以降) if any of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • T(削除されたり、デストラクタにアクセスできなくしている)破壊することはできません非静的データメンバを持っています
    Original:
    T has a non-static data member that cannot be destructed (has deleted or inaccessible destructor)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T(削除、またはアクセスできないデストラクタた)破壊することはできません直接、仮想基底クラスを持っています
    Original:
    T has direct or virtual base class that cannot be destructed (has deleted or inaccessible destructors)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T組合であり、非自明なデストラクタ(C++11およびそれ以降)持つバリアント部材を有する
    Original:
    T is a union and has a variant member with non-trivial destructor (C++11およびそれ以降)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 暗黙的に宣言されたデストラクタがvirtualである(基底クラスに仮想デストラクタを持っているため)、メモリ解放関数(operator delete()曖昧への呼び出しの結果、削除、またはアクセスできない機能.
    Original:
    The implicitly-declared destructor is virtual (because the base class has a virtual destructor) and the lookup for the deallocation function (operator delete() results in a call to ambiguous, deleted, or inaccessible function.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[編集] トリビアルデストラクタ

のルックアップ。クラスの暗黙的に宣言されたデストラクタTのすべての非静的データメンバは
Original:
The implicitly-declared destructor for class T is trivial if all of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • デストラクタは仮想ではない(つまり、基本クラスのデストラクタは仮想ではありません)
    Original:
    The destructor is not virtual (that is, the base class destructor is not virtual)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • すべての直接基底クラスに仮想デストラクタを持って
    Original:
    All direct base classes have virtual destructors
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • :以下のすべての条件が真であれば自明である。。。クラス型(またはクラス型の配列)は仮想デストラクタを持っています
    Original:
    All non-static data members of class type (or array of class type) have virtual destructors
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
些細なデストラクタがアクションを実行しませんデストラクタです。些細なデストラクタを持つオブジェクトがdelete-expressionを必要とせず、単に自分のストレージの割り当てを解除することによって、これを廃棄することができる。すべてC言語(POD型)と互換性のあるデータ·タイプは、些細なことに壊れやすいです.
Original:
A trivial destructor is a destructor that performs no action. Objects with trivial destructors don't require a delete-expression and may be disposed of by simply deallocating their storage. All data types compatible with the C language (POD types) are trivially destructible.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] デストラクタを暗黙的に定義された

暗黙的に宣言されたデストラクタが削除されたり、些細なことではないされている場合、それはコンパイラによって(、関数本体が生成され、コンパイルされている)が定義されている。この暗黙的に定義されたデストラクタが空のボディを持っている.
Original:
If the implicitly-declared destructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler. This implicitly-defined destructor has an empty body.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 破壊シーケンス

両方のユーザ定義または暗黙的に定義されたデストラクタでは、デストラクタの本体が実行された後、コンパイラは、クラスのすべての非静的な非変異体のメンバーのデストラクタを呼び出し、このオブジェクトは最も由来のものである場合、宣言の逆の順序で、それは、その後の建設とは逆の順序(順番に彼らのメンバーとその基底クラスなどのデストラクタを呼んでいる)のすべての直接基底クラスのデストラクタを呼び出し、クラス、それはすべての仮想基地のデストラクタを呼び出し.
Original:
For both user-defined or implicitly-defined destructors, after the body of the destructor is executed, the compiler calls the destructors for all non-static non-variant members of the class, in reverse order of declaration, then it calls the destructors of all direct base classes in reverse order of construction (which in turn call the destructors of their members and their base classes, etc), and then, if this object is of most-derived class, it calls the destructors of all virtual bases.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
デストラクタが直接呼び出された場合であっても(例えばobj.~Foo();)、~Foo()でreturn文が直ちに呼び出し元に制御を戻すことはありません:それはすべてのこれらの部材とベースデストラクタが最初に呼び出されます。 。。基底クラスのデストラクタがvirtualでない限り
Original:
Even when the destructor is called directly (e.g. obj.~Foo();), the return statement in ~Foo() does not return control to the caller immediately: it calls all those member and base destructors first.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 仮想デストラクタ

ベースにポインタを使用してオブジェクトを削除すると、未定義の動作を呼び出します
Original:
Deleting an object through pointer to base invokes undefined behavior unless the destructor in the base class is virtual:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

class Base {
 public:
    virtual ~Base() {}
};
class Derived : public Base {};
Base* b = new Derived;
delete b; // safe

共通のガイドラインは、基本クラスのデストラクタがeither public and virtual or protected and nonvirtualなければならないということです
Original:
A common guideline is that a destructor for a base class must be either public and virtual or protected and nonvirtual
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 純粋仮想デストラクタ

デストラクタは純粋仮想宣言することができます。。 。派生クラスが破棄されたときに、すべてのベースクラスのデストラクタが常に呼び出されているので、抽象なされる必要があるが、純粋仮想で宣言することができ、他の適切な機能を持っていない基本クラスで例えばこのようなデストラクタは、定義されている必要があります
Original:
A destructor may be declared pure virtual, for example in a base class which needs to be made abstract, but has no other suitable functions that could be declared pure virtual. Such destructor must have a definition, since all base class destructors are always called when the derived class is destroyed:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

class AbstractBase {
 public:
    virtual ~AbstractBase() = 0;
};
AbstractBase::~AbstractBase() {}
class Derived : public AbstractBase {};
// AbstractBase obj;   // compiler error
Derived obj;           // OK

[編集]

#include <iostream>
 
struct A
{
    int i;
 
    A ( int i ) : i ( i ) {}
 
    ~A()
    {
        std::cout << "~a" << i << std::endl;
    }
};
 
int main()
{
    A a1(1);
    A* p;
 
    { // nested scope
        A a2(2);
        p = new A(3);
    } // a2 out of scope
 
    delete p; // calls the destructor of a3
}

Output:

~a2
~a3
~a1