C 新特性 強制轉換const cast

2021-10-20 10:44:04 字數 387 閱讀 1402

//c++新特性 強制轉換const_cast

/* 強制型別轉換時具有一定風險的,有的轉換並不一定安全,如果把整數數值轉換成指標,把基類指標轉換成派生指標

把函式指標轉換成另外一種函式指標,把常量指標轉換成非常量指標等

1.const_cast 只針對指標,引用,this 去除const屬性

2.static_cast

3.reinterpret_cast

4.dynamic_cast

*/class ctest

void foo(int ntest) const

public:

int m_ntest;

};int main()

C 強制轉換

c風格的強制型別轉換 type cast 很簡單,不管什麼型別的轉換統統是 type b type a c 風格的型別轉換提供了4種型別轉換操作符來應對不同場合的應用。const cast,字面上理解就是去const屬性。static cast,命名上理解是靜態型別轉換。如int轉換成char。dy...

c 強制轉換

隱式轉換 char int 顯示轉換 c風格轉換 base b new test test t test b c 風格 static cast 編譯時檢查,如果不是派生關係,則編譯錯誤 dynamic cast 執行時檢查,如果不是派生關係,則返回null rtti reinterpret cast...

c 強制轉換

c 強制轉換總結 c語言的強制型別轉換,主要用於基礎資料之間的轉換 語法規則 type id expression 轉換格式1 type id expression 轉換格式2 c 除了c語言的強制型別轉換外,新增了四種強制型別轉換 static cast dynamic cast const ca...