猿问

请问这里的const_cast是什么用法?

const_cast不是去除底层const的作用吗,这里怎么反倒显示转化为const 引用了

慕斯709654
浏览 153回答 1
1回答

Helenr

const_cast conversionConverts between types with different cv-qualification.Syntaxconst_cast <&nbsp;new_type&nbsp;> (&nbsp;expression&nbsp;) &nbsp; &nbsp;Returns a value of type&nbsp;new_type.ExplanationOnly the following conversions can be done with&nbsp;const_cast. In particular, only&nbsp;const_cast&nbsp;may be used to cast away (remove) constness or volatility.1)&nbsp;Two possibly multilevel pointers to the same type may be converted between each other, regardless of cv-qualifiers at each level.2)&nbsp;lvalue of any type&nbsp;T&nbsp;may be converted to a lvalue or rvalue reference to the same type&nbsp;T, more or less cv-qualified. Likewise, an rvalue may be converted to a more or less cv-qualified rvalue reference. The result of a reference&nbsp;const_cast&nbsp;refers to the original object&nbsp;if&nbsp;expression&nbsp;is a glvalue and to the&nbsp;materialized temporaryotherwise&nbsp;(since C++17).3)&nbsp;Same rules apply to possibly multilevel pointers to data members&nbsp;and possibly multilevel pointers to arrays of known and unknown bound (arrays to cv-qualified elements are considered to be cv-qualified themselves)&nbsp;(since C++17)4)&nbsp;null pointer value may be converted to the null pointer value of&nbsp;new_typeAs with all cast expressions, the result is:an lvalue if&nbsp;new_type&nbsp;is an lvalue reference type or an rvalue reference to function type;an xvalue if&nbsp;new_type&nbsp;is an rvalue reference to object type;a prvalue otherwise.NotesPointers to functions and pointers to member functions are not subject to&nbsp;const_castconst_cast&nbsp;makes it possible to form a reference or pointer to non-const type that is actually referring to a&nbsp;const object&nbsp;or a reference or pointer to non-volatile type that is actually referring to a&nbsp;volatile object. Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile&nbsp;glvalue&nbsp;results in undefined behavior.Keywordsconst_castExampleRun this code1#include&nbsp;<iostream>&nbsp;struct&nbsp;type&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;type()&nbsp;:i(3)&nbsp;{}&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;m1(int&nbsp;v)&nbsp;const&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;this->i&nbsp;=&nbsp;v;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;compile&nbsp;error:&nbsp;this&nbsp;is&nbsp;a&nbsp;pointer&nbsp;to&nbsp;const&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const_cast<type*>(this)->i&nbsp;=&nbsp;v;&nbsp;//&nbsp;OK&nbsp;as&nbsp;long&nbsp;as&nbsp;the&nbsp;type&nbsp;object&nbsp;isn't&nbsp;const&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i;};&nbsp;int&nbsp;main()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i&nbsp;=&nbsp;3;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;i&nbsp;is&nbsp;not&nbsp;declared&nbsp;const&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;int&&nbsp;cref_i&nbsp;=&nbsp;i;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const_cast<int&>(cref_i)&nbsp;=&nbsp;4;&nbsp;//&nbsp;OK:&nbsp;modifies&nbsp;i&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;<<&nbsp;"i&nbsp;=&nbsp;"&nbsp;<<&nbsp;i&nbsp;<<&nbsp;'\n';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type&nbsp;t;&nbsp;//&nbsp;note,&nbsp;if&nbsp;this&nbsp;is&nbsp;const&nbsp;type&nbsp;t;,&nbsp;then&nbsp;t.m1(4);&nbsp;is&nbsp;UB&nbsp;&nbsp;&nbsp;&nbsp;t.m1(4);&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;<<&nbsp;"type::i&nbsp;=&nbsp;"&nbsp;<<&nbsp;t.i&nbsp;<<&nbsp;'\n';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;int&nbsp;j&nbsp;=&nbsp;3;&nbsp;//&nbsp;j&nbsp;is&nbsp;declared&nbsp;const&nbsp;&nbsp;&nbsp;&nbsp;int*&nbsp;pj&nbsp;=&nbsp;const_cast<int*>(&j);&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;*pj&nbsp;=&nbsp;4;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;undefined&nbsp;behavior!&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;(type::*mfp)(int)&nbsp;const&nbsp;=&nbsp;&type::m1;&nbsp;//&nbsp;pointer&nbsp;to&nbsp;member&nbsp;function//&nbsp;&nbsp;const_cast<void(type::*)(int)>(mfp);&nbsp;//&nbsp;compiler&nbsp;error:&nbsp;const_cast&nbsp;does&nbsp;not&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;work&nbsp;on&nbsp;function&nbsp;pointers&nbsp;&nbsp;}Output:i = 4 type::i = 4&nbsp;
随时随地看视频慕课网APP
我要回答