c++ const 轉非const

#include <iostream>
#include <stdio.h>
using namespace std;

struct DexHeader{
	int off;
};
int main()
{
	struct DexHeader dexHeader;
	dexHeader.off = 3;
	const DexHeader* lpDexHeader = &dexHeader;
	//此處發生一次淺拷貝
	DexHeader currentDexHeader = *lpDexHeader;
	printf("0x%x\n",(unsigned long long)lpDexHeader);
	printf("0x%x\n",(unsigned long long)&currentDexHeader);

	//純粹的去掉const屬性
	printf("0x%x\n",const_cast<DexHeader*>(lpDexHeader));
	return 0;
}

結果

0x172975a0
0x172975b0
0x172975a0

const_cast用來丟棄變量的const聲明,但不能改變變量所指向的對象的const屬性。即:const_cast用於原本非const的對象;如果用於原本const的對象,結果不可預知(C++語言未對此種情況進行規定)

例子:

#include <iostream>

using namespace std;

int main()
{
    const int a = 5;                 //主要是這裏是const,在數據區
    const int * p = &a;              //這裏指向的實際是棧的一個新變量
    int *p_var = NULL;
    
    p_var = const_cast <int*>(p);   //強轉爲非const指針
    cout << a <<endl;
    *p_var = 10;    //重新賦值
    cout << "*p=" << *p << endl;         //輸出10
    cout << "*p_var=" << *p_var << endl; //輸出10
    cout << "a=" << a << endl;           //輸出5
    system("pause");

    return 0;
}

結果:

5
*p=10
*p_var=10
a=5

彙編代碼:

.text:004114C0
.text:004114C0
.text:004114C0 ; Attributes: bp-based frame
.text:004114C0
.text:004114C0 ; int __cdecl main()
.text:004114C0 _main proc near
.text:004114C0
.text:004114C0 var_E4= byte ptr -0E4h
.text:004114C0 p_var= dword ptr -20h
.text:004114C0 p= dword ptr -14h
.text:004114C0 a= dword ptr -8
.text:004114C0
.text:004114C0 push    ebp
.text:004114C1 mov     ebp, esp
.text:004114C3 sub     esp, 0E4h
.text:004114C9 push    ebx
.text:004114CA push    esi
.text:004114CB push    edi
.text:004114CC lea     edi, [ebp+var_E4]
.text:004114D2 mov     ecx, 39h
.text:004114D7 mov     eax, 0CCCCCCCCh
.text:004114DC rep stosd
.text:004114DE mov     [ebp+a], 5
.text:004114E5 lea     eax, [ebp+a]
.text:004114E8 mov     [ebp+p], eax
.text:004114EB mov     [ebp+p_var], 0
.text:004114F2 mov     eax, [ebp+p]
.text:004114F5 mov     [ebp+p_var], eax
.text:004114F8 mov     esi, esp
.text:004114FA mov     eax, ds:__imp_?endl@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z ; std::endl(std::basic_ostream<char,std::char_traits<char>> &)
.text:004114FF push    eax
.text:00411500 mov     edi, esp
.text:00411502 push    5
.text:00411504 mov     ecx, ds:__imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@[email protected] ; std::basic_ostream<char,std::char_traits<char>> std::cout
.text:0041150A call    ds:__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@H@Z ; std::basic_ostream<char,std::char_traits<char>>::operator<<(int)
.text:00411510 cmp     edi, esp
.text:00411512 call    j___RTC_CheckEsp
.text:00411517 mov     ecx, eax
.text:00411519 call    ds:__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@P6AAAV01@AAV01@@Z@Z ; std::basic_ostream<char,std::char_traits<char>>::operator<<(std::basic_ostream<char,std::char_traits<char>> & (*)(std::basic_ostream<char,std::char_traits<char>> &))
.text:0041151F cmp     esi, esp
.text:00411521 call    j___RTC_CheckEsp
.text:00411526 mov     eax, [ebp+p_var]
.text:00411529 mov     dword ptr [eax], 0Ah
.text:0041152F mov     esi, esp
.text:00411531 mov     eax, ds:__imp_?endl@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z ; std::endl(std::basic_ostream<char,std::char_traits<char>> &)
.text:00411536 push    eax
.text:00411537 mov     edi, esp
.text:00411539 mov     ecx, [ebp+p]
.text:0041153C mov     edx, [ecx]
.text:0041153E push    edx
.text:0041153F push    offset _Val     ; "*p="
.text:00411544 mov     eax, ds:__imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@[email protected] ; std::basic_ostream<char,std::char_traits<char>> std::cout
.text:00411549 push    eax             ; _Ostr
.text:0041154A call    j_??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char const *)
.text:0041154F add     esp, 8
.text:00411552 mov     ecx, eax
.text:00411554 call    ds:__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@H@Z ; std::basic_ostream<char,std::char_traits<char>>::operator<<(int)
.text:0041155A cmp     edi, esp
.text:0041155C call    j___RTC_CheckEsp
.text:00411561 mov     ecx, eax
.text:00411563 call    ds:__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@P6AAAV01@AAV01@@Z@Z ; std::basic_ostream<char,std::char_traits<char>>::operator<<(std::basic_ostream<char,std::char_traits<char>> & (*)(std::basic_ostream<char,std::char_traits<char>> &))
.text:00411569 cmp     esi, esp
.text:0041156B call    j___RTC_CheckEsp
.text:00411570 mov     esi, esp
.text:00411572 mov     eax, ds:__imp_?endl@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z ; std::endl(std::basic_ostream<char,std::char_traits<char>> &)
.text:00411577 push    eax
.text:00411578 mov     edi, esp
.text:0041157A mov     ecx, [ebp+p_var]
.text:0041157D mov     edx, [ecx]
.text:0041157F push    edx
.text:00411580 push    offset aPVar    ; "*p_var="
.text:00411585 mov     eax, ds:__imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@[email protected] ; std::basic_ostream<char,std::char_traits<char>> std::cout
.text:0041158A push    eax             ; _Ostr
.text:0041158B call    j_??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char const *)
.text:00411590 add     esp, 8
.text:00411593 mov     ecx, eax
.text:00411595 call    ds:__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@H@Z ; std::basic_ostream<char,std::char_traits<char>>::operator<<(int)
.text:0041159B cmp     edi, esp
.text:0041159D call    j___RTC_CheckEsp
.text:004115A2 mov     ecx, eax
.text:004115A4 call    ds:__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@P6AAAV01@AAV01@@Z@Z ; std::basic_ostream<char,std::char_traits<char>>::operator<<(std::basic_ostream<char,std::char_traits<char>> & (*)(std::basic_ostream<char,std::char_traits<char>> &))
.text:004115AA cmp     esi, esp
.text:004115AC call    j___RTC_CheckEsp
.text:004115B1 mov     esi, esp
.text:004115B3 mov     eax, ds:__imp_?endl@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z ; std::endl(std::basic_ostream<char,std::char_traits<char>> &)
.text:004115B8 push    eax
.text:004115B9 mov     edi, esp
.text:004115BB push    5
.text:004115BD push    offset aA       ; "a="
.text:004115C2 mov     ecx, ds:__imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@[email protected] ; std::basic_ostream<char,std::char_traits<char>> std::cout
.text:004115C8 push    ecx             ; _Ostr
.text:004115C9 call    j_??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,char const *)
.text:004115CE add     esp, 8
.text:004115D1 mov     ecx, eax
.text:004115D3 call    ds:__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@H@Z ; std::basic_ostream<char,std::char_traits<char>>::operator<<(int)
.text:004115D9 cmp     edi, esp
.text:004115DB call    j___RTC_CheckEsp
.text:004115E0 mov     ecx, eax
.text:004115E2 call    ds:__imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@P6AAAV01@AAV01@@Z@Z ; std::basic_ostream<char,std::char_traits<char>>::operator<<(std::basic_ostream<char,std::char_traits<char>> & (*)(std::basic_ostream<char,std::char_traits<char>> &))
.text:004115E8 cmp     esi, esp
.text:004115EA call    j___RTC_CheckEsp
.text:004115EF mov     esi, esp
.text:004115F1 push    offset Command  ; "pause"
.text:004115F6 call    ds:__imp__system
.text:004115FC add     esp, 4
.text:004115FF cmp     esi, esp
.text:00411601 call    j___RTC_CheckEsp
.text:00411606 xor     eax, eax
.text:00411608 push    edx
.text:00411609 mov     ecx, ebp        ; frame
.text:0041160B push    eax
.text:0041160C lea     edx, v          ; v
.text:00411612 call    j_@_RTC_CheckStackVars@8 ; _RTC_CheckStackVars(x,x)
.text:00411617 pop     eax
.text:00411618 pop     edx
.text:00411619 pop     edi
.text:0041161A pop     esi
.text:0041161B pop     ebx
.text:0041161C add     esp, 0E4h
.text:00411622 cmp     ebp, esp
.text:00411624 call    j___RTC_CheckEsp
.text:00411629 mov     esp, ebp
.text:0041162B pop     ebp
.text:0041162C retn

 

發佈了265 篇原創文章 · 獲贊 164 · 訪問量 218萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章