64,660
社区成员
发帖
与我相关
我的任务
分享
6: int* ptr;
7: const int a=9;
00414F18 mov dword ptr [ebp-8],9//存入 a
8: ptr=(int*)&a;
00414F1F lea eax,[ebp-8]//取地址
00414F22 mov dword ptr [ebp-4],eax
9: *ptr=19;
00414F25 mov ecx,dword ptr [ebp-4]//取ptr指向的地址
00414F28 mov dword ptr [ecx],13h//存入
10: printf("%d\n%d\n",*ptr,a);
00414F2E push 9//注意,看这里!!!!!!!!!!!!!!
00414F30 mov edx,dword ptr [ebp-4]//ptr
00414F33 mov eax,dword ptr [edx]//
00414F35 push eax
00414F36 push offset string "%d\n%d\n" (0042c9fc)
00414F3B call printf (00415160)
00414F40 add esp,0Ch
const volatile int a=9;
The following demonstrates the use of the const_cast operator:
#include <iostream>
using namespace std;
void f(int* p) {
cout << *p << endl;
}
int main(void) {
const int a = 10;
const int* b = &a;
// Function f() expects int*, not const int*
// f(b);
int* c = const_cast<int*>(b);
f(c);
// Lvalue is const
// *b = 20;
// Undefined behavior
// *c = 30;
int a1 = 40;
const int* b1 = &a1;
int* c1 = const_cast<int*>(b1);
// Integer a1, the object referred to by c1, has
// not been declared const
*c1 = 50;
return 0;
}
/*
The compiler will not allow the function call f(b). Function f() expects a pointer to an int, not a const int. The statement int* c = const_cast<int>(b) returns a pointer c that refers to a without the const qualification of a. This process of using const_cast to remove the const qualification of an object is called casting away constness. Consequently the compiler will allow the function call f(c).
The compiler would not allow the assignment *b = 20 because b points to an object of type const int. The compiler will allow the *c = 30, but the behavior of this statement is undefined. If you cast away the constness of an object that has been explicitly declared as const, and attempt to modify it, the results are undefined.
However, if you cast away the constness of an object that has not been explicitly declared as const, you can modify it safely. In the above example, the object referred to by b1 has not been declared const, but you cannot modify this object through b1. You may cast away the constness of b1 and modify the value to which it refers.
*/
Const cast
Sometimes, you have a const variable and you really want to pass it into a function that you are certain won't modify it. But that function doesn't declare its argument as const. (This might happen, for instance, if a C library function like strlen were declared without using const.) Fortunately, if you know that you are safe in passing a const variable into a function that doesn't explicitly indicate that it will not change the data, then you can use a const_cast in order to temporarily strip away the const-ness of the object.
Const casts look like regular typecasts in C++, except that they can only be used for casting away constness (or volatile-ness) but not converting between types or casting down a class hierarchy.
// a bad version of strlen that doesn't declare its argument const
int bad_strlen (char *x)
{
strlen( x );
}
// note that the extra const is actually implicit in this declaration since
// string literals are constant
const char *x = "abc";
// cast away const-ness for our strlen function
bad_strlen( const_cast<char *>(x) );
Note that you can also use const_cast to go the other way--to add const-ness--if you really wanted to.
int* ptr;
const int a=9;
0041262E mov dword ptr [a],9
ptr=const_cast<int*>(&a);
00412635 lea eax,[a]
00412638 mov dword ptr [ptr],eax
*ptr=19;
0041263B mov eax,dword ptr [ptr]
0041263E mov dword ptr [eax],13h
printf("%d\n%d\n",*ptr,a);
00412644 mov esi,esp
00412646 push 9
00412648 mov eax,dword ptr [ptr]
0041264B mov ecx,dword ptr [eax]
0041264D push ecx
0041264E push offset string "%d\n%d\n" (41B8ACh)
int* ptr;
const volatile int a=9;
0041262E mov dword ptr [a],9
ptr=const_cast<int*>(&a);
00412635 lea eax,[a]
00412638 mov dword ptr [ptr],eax
*ptr=19;
0041263B mov eax,dword ptr [ptr]
0041263E mov dword ptr [eax],13h
printf("%d\n%d\n",*ptr,a);
00412644 mov esi,esp
00412646 mov eax,dword ptr [a]
00412649 push eax
0041264A mov ecx,dword ptr [ptr]
0041264D mov edx,dword ptr [ecx]
0041264F push edx