C++中const-cast與reinterpret-cast運算符的用法
C++中const_cast與reinterpret_cast運算符的用法,經常被用於表達式中的類型轉換,下面是小編分享的運算符的用法,一起來看一下吧。
const_cast 運算符
從類中移除 const、volatile 和 __unaligned 特性。
語法
const_cast <
type-id
> (
expression
)
備註
指向任何對象類型的指針或指向數據成員的指針可顯式轉換爲完全相同的類型(const、volatile 和 __unaligned 限定符除外)。對於指針和引用,結果將引用原始對象。對於指向數據成員的指針,結果將引用與指向數據成員的原始(未強制轉換)的指針相同的成員。根據引用對象的類型,通過生成的指針、引用或指向數據成員的.指針的寫入操作可能產生未定義的行爲。
您不能使用 const_cast 運算符直接重寫常量變量的常量狀態。
const_cast 運算符將 null 指針值轉換爲目標類型的 null 指針值。
// expre_const_cast_
// compile with: /EHsc
#include <iostream>
using namespace std;
class CCTest {
public:
void setNumber( int );
void printNumber() const;
private:
int number;
};
void CCTest::setNumber( int num ) { number = num; }
void CCTest::printNumber() const {
cout << "Before: " << number;
const_cast< CCTest * >( this )->number--;
cout << "After: " << number;
}
int main() {
CCTest X;
umber( 8 );
tNumber();
}
在包含 const_cast 的行中,this 指針的數據類型爲 const CCTest *。 const_cast 運算符會將 this 指針的數據類型更改爲 CCTest *,以允許修改成員 number。強制轉換僅對其所在的語句中的其餘部分持續。
reinterpret_cast 運算符
允許將任何指針轉換爲任何其他指針類型。也允許將任何整數類型轉換爲任何指針類型以及反向轉換。
語法
reinterpret_cast < type-id > ( expression )
備註
濫用 reinterpret_cast 運算符可能很容易帶來風險。除非所需轉換本身是低級別的,否則應使用其他強制轉換運算符之一。
reinterpret_cast 運算符可用於 char* 到 int* 或 One_class* 到 Unrelated_class* 之類的轉換,這本身並不安全。
reinterpret_cast 的結果不能安全地用於除強制轉換回其原始類型以外的任何用途。在最好的情況下,其他用途也是不可移植的。
reinterpret_cast 運算符不能丟掉 const、volatile 或 __unaligned 特性。有關移除這些特性的詳細信息,請參閱 const_cast Operator。
reinterpret_cast 運算符將 null 指針值轉換爲目標類型的 null 指針值。
reinterpret_cast 的一個實際用途是在哈希函數中,即,通過讓兩個不同的值幾乎不以相同的索引結尾的方式將值映射到索引。
#include <iostream>
using namespace std;
// Returns a hash code based on an address
unsigned short Hash( void *p ) {
unsigned int val = reinterpret_cast<unsigned int>( p );
return ( unsigned short )( val ^ (val >> 16));
}
using namespace std;
int main() {
int a[20];
for ( int i = 0; i < 20; i++ )
cout << Hash( a + i ) << endl;
}
Output:
64641
64645
64889
64893
64881
64885
64873
64877
64865
64869
64857
64861
64849
64853
64841
64845
64833
64837
64825
64829
reinterpret_cast 允許將指針視爲整數類型。結果隨後將按位移位並與自身進行“異或”運算以生成唯一的索引(具有唯一性的概率非常高)。該索引隨後被標準 C 樣式強制轉換截斷爲函數的返回類型。
相關文章
-
四級作文:Create a Conservation-Oriented Campus
四級作文:Create a Conservation-Oriented Campus,四級作文:Create a Conservation-Oriented Campus範文,免費提供給大家交流參考! Create a Conservation-Oriented Campus1.建設節約型校園十分重要2.原因是…&he -
A Letter to Univsity Pesident about the Canteen Service on C
dear president:i am a student who lives on campus and uses canteen services everyday. i would love to share my opinion on the services i received. also, i have some suggestions on how to improve the c -
The relationship between theory and practice大學英語作文
We all know that books are a source of there are those who think what the book contains must be is stupid to follow the books without any the old saying goes,to believe everything in books is worse -
Protecting water resources英語作文
Water is to us what air is to birds. As our daily necessity, we can't live without water. Nevertheless, there do exist many severe water pollutions all over the world. As a result, it's no dou -
託福英語寫作範文:Examinations exert a pernicious influence on educat
We might marvel at the progress made in every field of study, but the methods of testing a person’s knowledge and ability remain as primitive as ever they were. It really is extraordinary that after -
script runat server 在ASP.NET中的用法
本文實例講述了中的用法,分享給大家供大家參考。具體如下: 在的頁面中,可以在代碼標籤中通過添加代碼。其實,這裏的代碼和文件中的代碼地位是相同的',都是在服務器端執行的。 例: 複製代碼 代碼如下: void Select_Change(O -
我的英語演講稿living at present is my top concern
ladies and gentleman,good evening!it's my great honor to present this speech name is Peace and I am from class 1 of Teaching Chinese as a foreign language. My topic today is "Living at Present is -
分享SAT閱讀練習題:Reading Comprehension Test 14
SAT閱讀練習題:Reading Comprehension Test 14Much of what goes by the name of pleasure is simply an effort to destroy consciousness. If one started by asking, what is man? what are his needs? how can he -
英語閱讀:VIT declines to acquire Ang Mo Kio property
像在北方地區大棚內的黑坑較爲多見,自然不會受到多少自然因素的影響,可遮風擋雨,現在的垂釣環境也是越來越好內部還設有地暖、大燈,大大提高了釣魚的舒適度。冬季來臨,氣溫突降,越來越多的北方釣友開始轉戰大棚,高手在線嘉賓 -
Certificate Craze on Campus英語四級作文
In recent years, to get a certificate has become a new craze among college students. Just randomly ask a student on campus what he or she is busy doing, quite possibly, you may get the answer that he