c++ const 转非const

本文深入探讨了C++中const_cast操作符的使用场景和潜在风险,通过实例展示了如何去除变量的const属性,以及在不同上下文中const_cast可能带来的后果。特别关注了const_cast在指针操作中的应用,包括其对指针所指向对象const属性的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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@@@1@A.gap0 ; 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@@@1@A.gap0 ; 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@@@1@A.gap0 ; 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@@@1@A.gap0 ; 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值