名前空間
変種
操作

dynamic_cast conversion

提供: 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:
Safely converts pointers and references to classes up, down, and sideways along the inheritance hierarchy.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集] 構文

dynamic_cast < new_type > ( expression )
タイプnew_typeの値を返すか、例外をスローします.
Original:
Returns a value of type new_type or throws an exception.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 説明

のみ、次の変換は、このような変換は const性を捨てたい場合を除き、dynamic_castで行うことができますや'ボラティリティ.
Original:
Only the following conversions can be done with dynamic_cast, except when such conversions would cast away constness or volatility.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1)
expressionの型が正確にnew_typeまたはnew_type未満のcv-修飾バージョンである場合、結果はexpressionです.
Original:
If the type of expression is the exactly new_type or a less cv-qualified version of new_type, the result is expression.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
expressionの値がヌルポインタの値である場合、結果はタイプnew_typeのnullポインタ値です
Original:
If the value of expression is the null pointer value, the result is the null pointer value of type new_type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
new_typeBaseへのポインタまたは参照であり、expressionのタイプがDerivedBaseのユニークな、アクセス可能な基底クラスですDerived、ポインタや参照である場合、結果はBase内のポインタまたはDerivedクラスのサブオブジェクトへの参照です。オブジェクトが指摘またはexpressionによって識別されます。 (注:暗黙的なキャストとはstatic_castも同様にこの変換を行うことができます)
Original:
If new_type is a pointer or reference to Base, and the type of expression is a pointer or reference to Derived, where Base is a unique, accessible base class of Derived, the result is a pointer or reference to the Base class subobject within the Derived object pointed or identified by expression. (note: implicit cast and static_cast can perform this conversion as well)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
expressionが多相型へのポインタまたは参照であり、new_typevoidへのポインタである場合、結果はexpressionによって指し示さまたは参照ほとんどの派生オブジェクトへのポインタです。.
Original:
If expression is a pointer or reference to a polymorphic type, and new_type is a pointer to void, the result is a pointer to the most derived object pointed or referenced by expression.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5)
expressionは多相型Baseへのポインタまたは参照であり、new_typeは型へのポインタまたは参照である場合Derivedランタイムチェックが実行されます
Original:
If expression is a pointer or reference to a polymorphic type Base, and new_type is a pointer or reference to the type Derived a run-time check is performed:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
a)
expressionによって識別/指さ最も派生したオブジェクトが検査されます。 expressionの公衆基地へDerived点/参照されるのは、そのオブジェクトとした場合において、及びDerivedタイプの1つのサブオブジェクトがexpressionによって識別/指さサブオブジェクトから派生している場合、キャストポイントの結果は/そのDerivedサブオブジェクトを指します。 (これは "ダウンキャスト"として知られています)
Original:
The most derived object pointed/identified by expression is examined. If, in that object, expression points/referes to a public base of Derived, and if only one subobject of Derived type is derived from the subobject pointed/identified by expression, then the result of the cast points/refers to that Derived subobject. (this is known as the "downcast")
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
b)
expressionポイントは/ほとんどの派生オブジェクトのパブリックベースを参照している場合、それ以外の場合、及び、simultanously、ほとんどの派生オブジェクト(これは知られており、キャストの点の結果を型Derivedの一義的な公共基底クラスを持っている/そのDerivedを指しとして "sidecast")
Original:
Otherwise, if expression points/refers to a public base of the most derived object, and, simultanously, the most derived object has an unambiguous public base class of type Derived, the result of the cast points/refers to that Derived (this is known as the "sidecast")
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
c)
それ以外の場合は、ランタイムチェックは失敗します。 dynamic_castは、ポインタで使用されている場合は、タイプnew_typeのnullポインタの値が返されます。それは参照に使用された場合は、例外がスローされますstd::bad_cast.
Original:
Otherwise, the runtime check fails. If the dynamic_cast is used on pointers, the null pointer value of type new_type is returned. If it was used on references, the exception std::bad_cast is thrown.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
6)
dynamic_castがコンストラクタまたはデストラクタ(直接または間接的に)で使用され、expression、建設/破壊の下で現在のオブジェクトを指している場合、そのオブジェクトは最も派生したオブジェクトであると見なされます。 new_typeは、建設の/デストラクタ自身のクラスまたはその拠点の一つへのポインタまたは参照されていない場合、動作は未定義です.
Original:
When dynamic_cast is used in a constructor or a destructor (directly or indirectly), and expression refers to the object that's currently under construction/destruction, the object is considered to be the most derived object. If new_type is not a pointer or reference to the construction's/destructor's own class or one of its bases, the behavior is undefined.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
他のキャスト式と同様に、結果は次のとおりです
Original:
Similar to other cast expressions, the result is:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • new_type場合左辺値は左辺値参照型(expressionは左辺値でなければなりません)です
    Original:
    an lvalue if new_type is an lvalue reference type (expression must be an lvalue)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • new_type場合はxValueは右辺値参照型(expressionが左辺または右辺値である場合があります)
    Original:
    an xvalue if new_type is an rvalue reference type (expression may be lvalue or rvalue)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • さもなければprvalue(この場合、new_typeがポインタ型である場合)
    Original:
    a prvalue otherwise (in this case, if new_type is a pointer type)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[編集] 注意してください

ダウンキャストは、実行時チェックのコストを回避static_cast、使用して実行されますが、プログラムがexpressionが指すオブジェクトがDerived間違いであることを、いくつかの他のロジックを介して、保証できる場合にのみ安全ですすることができます.
Original:
Downcast can also be performed with static_cast, which avoids the cost of the runtime check, but it's only safe if the program can guarantee, through some other logic, that the object pointed to by expression is definitely Derived.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] キーワード

dynamic_cast

[編集]

#include <iostream>
 
struct V {
    virtual void f() {};  // must be polymorphic to use runtime-checked dynamic_cast
};
struct A : virtual V {};
struct B : virtual V {
  B(V* v, A* a) {
    // casts during construction
    dynamic_cast<B*>(v); // well-defined: v of type V*, V base of B, results in B*
    dynamic_cast<B*>(a); // undefined behavior: a has type A*, A not a base of B
  }
};
struct D : A, B {
    D() : B((A*)this, this) { }
};
 
struct Base {
    virtual ~Base() {}
};
 
struct Derived: Base {
    virtual void name() {}
};
 
int main()
{
    D d; // the most derived object
    A& a = d; // upcast, dynamic_cast may be used, but unnecessary
    D& new_d = dynamic_cast<D&>(a); // downcast
    B& new_b = dynamic_cast<B&>(a); // sidecast
 
 
    Base* b1 = new Base;
    if(Derived* d = dynamic_cast<Derived*>(b1))
    {
        std::cout << "downcast from b1 to d successful\n";
        d->name(); // safe to call
    }
 
    Base* b2 = new Derived;
    if(Derived* d = dynamic_cast<Derived*>(b2))
    {
        std::cout << "downcast from b2 to d successful\n";
        d->name(); // safe to call
    }
 
    delete b1;
    delete b2;
}

Output:

downcast from b2 to d successful

[編集] も参照してください