Is "const Object* pA = new Object(1)" the same as "const Object A(1)"
in terms of creating const objects that should not be modified through
cast-to-non-const pointers. See the following example:
const int* createInt()
{
return new int(1);
}
void myFunc( const int* const_param )
{
cout << "const_para m == " << *const_param << endl;
int *non_const_para m = const_cast<int* >( const_param );
*non_const_para m = 999;
cout << "const_para m == " << *const_param << endl;
}
int main(int argc, char* argv[])
{
int a = 1;
const int b = 1;
int* c = new int(1);
const int* d = new int(1);
const int* e = createInt();
myFunc( &a ); // ok
myFunc( &b ); // bad
myFunc( c ); // ok
myFunc( d ); // bad (?)
myFunc( e ); // bad (?)
return 0;
}
in terms of creating const objects that should not be modified through
cast-to-non-const pointers. See the following example:
const int* createInt()
{
return new int(1);
}
void myFunc( const int* const_param )
{
cout << "const_para m == " << *const_param << endl;
int *non_const_para m = const_cast<int* >( const_param );
*non_const_para m = 999;
cout << "const_para m == " << *const_param << endl;
}
int main(int argc, char* argv[])
{
int a = 1;
const int b = 1;
int* c = new int(1);
const int* d = new int(1);
const int* e = createInt();
myFunc( &a ); // ok
myFunc( &b ); // bad
myFunc( c ); // ok
myFunc( d ); // bad (?)
myFunc( e ); // bad (?)
return 0;
}
Comment