W
want.to.be.professer
#include <iostream>
using namespace std;
// Check if N is constant
template <int N>
class ValidateConstInt{};
int main()
{
const int constValue = 12;
const int* pConstValue = &constValue;
int* pValue = const_cast <int*> ( &constValue );
ValidateConstInt <constValue> (); // Check if constValue is
constant
cout << "------------------------------------ " << endl;
cout << " begin: " << endl;
cout << "\t const int constValue = 12; " << endl;
cout << "\t const int* pConstValue = &constValue;"<< endl;
cout << "\t int* pValue = const_cast <int*> (&ConstValue);"<<
endl;
cout << endl;
cout << "------------------------------------ " << endl;
cout << " constValue = " << constValue << endl;
cout << " &constValue = " << &constValue << endl;
cout << " *pConstValue = " << *pConstValue << endl;
cout << " pConstValue = " << pConstValue << endl;
cout << " *pValue = " << *pValue << endl;
cout << " pValue = " << pValue << endl;
cout << endl;
*pValue = 1; // Change the value
cout << "------------------------------------- After *pValue = 1
" << endl;
cout << endl;
cout << " constValue = " << constValue << endl;
cout << " &constValue = " << &constValue << endl;
cout << " *pConstValue = " << *pConstValue << endl;
cout << " pConstValue = " << pConstValue << endl;
cout << " *pValue = " << *pValue << endl;
cout << " pValue = " << pValue << endl;
cout << "------------------------------------ " << endl;
return 0;
}
// ------------- Result:
------------------------------------
begin:
const int constValue = 12;
const int* pConstValue = &constValue;
int* pValue = const_cast <int*> (&ConstValue);
------------------------------------
constValue = 12
&constValue = 0012FF6C
*pConstValue = 12
pConstValue = 0012FF6C
*pValue = 12
pValue = 0012FF6C
------------------------------------- After *pValue = 1
constValue = 12 // not changed
&constValue = 0012FF6C
*pConstValue = 1 // have changed
pConstValue = 0012FF6C
*pValue = 1 // have changed
pValue = 0012FF6C
------------------------------------
The pConstValue and pValue both point to the ConstValue, and the value
access by the point is changed, but constValue has never changed ( and
it shoule not be a constant ). I can not explain, Can somebody help
me ?
using namespace std;
// Check if N is constant
template <int N>
class ValidateConstInt{};
int main()
{
const int constValue = 12;
const int* pConstValue = &constValue;
int* pValue = const_cast <int*> ( &constValue );
ValidateConstInt <constValue> (); // Check if constValue is
constant
cout << "------------------------------------ " << endl;
cout << " begin: " << endl;
cout << "\t const int constValue = 12; " << endl;
cout << "\t const int* pConstValue = &constValue;"<< endl;
cout << "\t int* pValue = const_cast <int*> (&ConstValue);"<<
endl;
cout << endl;
cout << "------------------------------------ " << endl;
cout << " constValue = " << constValue << endl;
cout << " &constValue = " << &constValue << endl;
cout << " *pConstValue = " << *pConstValue << endl;
cout << " pConstValue = " << pConstValue << endl;
cout << " *pValue = " << *pValue << endl;
cout << " pValue = " << pValue << endl;
cout << endl;
*pValue = 1; // Change the value
cout << "------------------------------------- After *pValue = 1
" << endl;
cout << endl;
cout << " constValue = " << constValue << endl;
cout << " &constValue = " << &constValue << endl;
cout << " *pConstValue = " << *pConstValue << endl;
cout << " pConstValue = " << pConstValue << endl;
cout << " *pValue = " << *pValue << endl;
cout << " pValue = " << pValue << endl;
cout << "------------------------------------ " << endl;
return 0;
}
// ------------- Result:
------------------------------------
begin:
const int constValue = 12;
const int* pConstValue = &constValue;
int* pValue = const_cast <int*> (&ConstValue);
------------------------------------
constValue = 12
&constValue = 0012FF6C
*pConstValue = 12
pConstValue = 0012FF6C
*pValue = 12
pValue = 0012FF6C
------------------------------------- After *pValue = 1
constValue = 12 // not changed
&constValue = 0012FF6C
*pConstValue = 1 // have changed
pConstValue = 0012FF6C
*pValue = 1 // have changed
pValue = 0012FF6C
------------------------------------
The pConstValue and pValue both point to the ConstValue, and the value
access by the point is changed, but constValue has never changed ( and
it shoule not be a constant ). I can not explain, Can somebody help
me ?