const_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:
Converts between types with different cv-qualification.
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.
Содержание |
[править] Синтаксис
const_cast < new_type > ( expression )
|
|||||||||
Возвращает значение типа
new_type.Original:
Returns a 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.
[править] Объяснение
Только следующие преобразования может быть сделано с const_cast. В частности, только const_cast могут быть использованы для отбросить (убрать) константность или волатильность.
Original:
Only the following conversions can be done with const_cast. In particular, only const_cast may be used to cast away (remove) 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)
Два возможности многоуровневых указателей на того же типа могут быть преобразованы друг с другом, независимо от CV-отборочные на каждом уровне.
Original:
Two possibly multilevel pointers to the same type may be converted between each other, regardless of cv-qualifiers at each level.
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)
именующее любого типа
T могут быть преобразованы в ссылки именующее или RValue к тому же T типа, более или менее CV-квалификации. Кроме того, RValue могут быть преобразованы в более или менее резюме квалифицированных RValue ссылки.Original:
lvalue of any type
T may be converted to a lvalue or rvalue reference to the same type T, more or less cv-qualified. Likewise, an rvalue may be converted to a more or less cv-qualified rvalue reference.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)
Те же правила применяются к возможности многоуровневых указателей на члены данных.
Original:
Same rules apply to possibly multilevel pointers to data members.
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)
нулевое значение указателя может быть преобразовано в нулевое значение указателя new_type
Original:
null pointer value may be converted to the null pointer value of 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.
Как и во всех литых выражения, результат
Original:
As with all 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 является тип именующее ссылку или RValue ссылка на функцию типаOriginal:an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;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 ссылку на объект типаOriginal:an xvalue if new_type is an rvalue reference to object type;The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - prvalue иначе.Original:a prvalue otherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[править] Notes
Указатели на функции и указатели на функции-члены не подлежат const_cast
Original:
Pointers to functions and pointers to member functions are not subject to const_cast
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.
Даже если const_cast может удалить константность от любого указателя или ссылки, используя полученный указатель или ссылку на запись в объект, который был объявлен const вызывает неопределенное поведение.
Original:
Even though const_cast may remove constness from any pointer or reference, using the resulting pointer or reference to write to an object that was declared const invokes undefined behavior.
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 type { type() :i(3) {} void m1(int v) const { // this->i = v; // compile error: this is a pointer to const const_cast<type*>(this)->i = v; // OK } int i; }; int main() { int i = 3; // i is not declared const const int& cref_i = i; const_cast<int&>(cref_i) = 4; // OK: modifies i std::cout << "i = " << i << '\n'; type t; t.m1(4); std::cout << "type::i = " << t.i << '\n'; const int j = 3; // j is declared const int* pj = const_cast<int*>(&j); *pj = 4; // undefined behavior! void (type::*mfp)(int) const = &type::m1; // pointer to member function // const_cast<void(type::*)(int)>(mfp); // compiler error: const_cast does not // work on function pointers }
Вывод:
i = 4 type::i = 4