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 樣式強制轉換截斷為函式的返回型別。
-
2017上半年計算機二級C語言考前模擬試題
為了幫助大家提高計算機考試成績,今天本站小編特地為大家收集了以下2017上半年計算機二級C語言考前模擬試題,歡迎學習!一、程式填空題給定程式中,函式fun的功能是:把形參S所指字串中最右邊的n個字元複製到形參t所指字元陣列中,形成一個新串。若s所指字串的長度...
-
在VS下如何執行C語言程式
微軟後來對VC6.0進行了升級,並更名為VisualStudio(簡稱VS),支援更多的程式語言,更加強大的功能,不過VisualStudio檔案很大,有2~3G左右,大部分功能初學者暫時不會用到;而且安裝繁瑣,需要將近半個小時的時間,也不容易解除安裝乾淨。VisualStudio還有一個缺點是佔用資源較多,不...
-
C語言順序結構知識歸納
C語言的順序結構裡面有哪些知識需要學習的呢,下面小編為大家歸納了C語言順序結構知識,歡迎大家閱讀!C語言順序結構知識歸納一、表示式語句、函式呼叫語句和空語句1.C語言的語句共分五大類:表示式語句、控制語句、函式呼叫語句、空語句和複合語句。2.表示式語句的...
-
2017計算機二級C語言考點解析
大家在備考計算機二級考試時要掌握考點,這樣複習才有方向,以下是本站小編整理的計算機二級C語言考點解析,更多計算機二級考試資訊請繼續關注應屆畢業生考試網。8.1指標與指標變數的概念,指標與地址運算子1.在C語言中,指標是指一個變數的地址,通過變數的地址″指...
相關文章
- 四級作文:Create a Conservation-Oriented Campus
- A Letter to Univsity Pesident about the Canteen Service on C
- Certificate Craze on Campus英語四級作文
- Protecting water resources英語作文
- 託福英語寫作範文:Examinations exert a pernicious influence on educat
- 我的英語演講稿living at present is my top concern
- script runat server 在ASP.NET中的用法
- The relationship between theory and practice大學英語作文
- 分享SAT閱讀練習題:Reading Comprehension Test 14
- 英語閱讀:VIT declines to acquire Ang Mo Kio property