[C++] 纯文本查看 复制代码
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i = 10;
const int *v = &i;
int *p;
p = const_cast <int *> (v);
*p = 15;
cout << i << endl;
return 0;
}
[C++] 纯文本查看 复制代码
#include "stdafx.h"
#include <iostream>
using namespace std;
void func(const int *p)
{
int *v;
//去掉 const 属性
v = const_cast<int *> (p);
*v = 100; //现在可以通过指针v来修改值了
}
int _tmain(int argc, _TCHAR* argv[])
{
int x = 99;
cout << "x before call: " << x << endl;
func(&x);
cout << "x after call: " << x << endl;
return 0;
}