dynamic_cast conversion
Материал из cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Безопасное преобразует указатели и ссылки на классы, вверх, вниз, вбок и по иерархии наследования.
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.
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.
You can help to correct and verify the translation. Click here for instructions.
[править] Объяснение
Только следующие преобразования может быть сделано с 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.
You can help to correct and verify the translation. Click here for instructions.
1)
Если тип expression является именно new_type или менее квалифицированных CV-версия new_type, в результате 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.
You can help to correct and verify the translation. Click here for instructions.
2)
Если значение expression является нулевое значение указателя, то результат будет нулевым значением указатель типа new_type
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.
You can help to correct and verify the translation. Click here for instructions.
3)
Если new_type представляет собой указатель или ссылку на
Base, и тип expression представляет собой указатель или ссылку на Derived, где Base является уникальным, доступный базовый класс 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.
You can help to correct and verify the translation. Click here for instructions.
4)
Если expression это указатель или ссылку на полиморфный тип, и
new_type является указателем на void, результатом является указатель на самый объект, производный от указали или ссылаться на 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.
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.
You can help to correct and verify the translation. Click here for instructions.
a)
Наиболее объект, производный от указали /, определенных expression рассматривается. Если в этом объекте, expression точек / referes к общественной базы
Derived, а если только один подобъект типа Derived выводится из подобъектов указали /, определенных 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.
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.
You can help to correct and verify the translation. Click here for instructions.
c)
В противном случае выполнения проверка не удалась. Если dynamic_cast используется на указатели, нулевое значение указателя типа new_type возвращается. Если он был использован на ссылки, за исключением 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.
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.
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.
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. - xvalue если new_type является тип RValue ссылка (expression может быть именующее или RValue)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.
[править] Примечание
Downcast также может быть выполнена с 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.
You can help to correct and verify the translation. Click here for instructions.
[править] Ключевые слова
[править] Пример
#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; }
Вывод:
downcast from b2 to d successful