# include <iostream>
usingnamespace std;
// Attempting to modify data through a
// nonconstant pointer to constant data.
void f( constint * ); // prototype
int main()
{
int y;
f( &y ); // f attempts illegal modification
}//end main
// xPtr cannot modify the value of constant variable to which it points
void f(constint *xPtr)
{
*xPtr = 100; // error: cannot modify a const object
} // end function f
#include <iostream>
usingnamespace std;
void f( constint * ); // prototype
int main()
{
int y;
f( &y); // f attempts illegal modification
cout << y << endl ;
system ("pause");
} // end main
// xPtr cannot modify the value of constant variable to which it points
void f( constint *xPtr )
{
constint y = 100;
xPtr = &y; // error: cannot modify a const object
} // end function f
const by definition means you cannot modify the object... so stop trying to modify it. If you need the function to modify it, then don't use the const keyword.
const on a pointer means you cannot change the pointer or the data it points too. Also, understand that data itself cannot be constant in the way you're thinking of it.
It's a type, a pointer has no notion of what constness its data has unless you specify its constness through what type the pointer is.
1 2
constint * pConstInteger; /* The data must be constant! */
int * pInteger; /* The data must *not* best constant! */
Mind you, modifying const data is NOT to be done. In some cases, this can cause a SEGMENTATION FAULT which is a big no-no.
#include <iostream>
usingnamespace std;
void f( constint * ); // prototype
int y;
int main()
{
f( &y); // f attempts illegal modification
cout << y << endl ;
system ("pause");
} // end main
// xPtr cannot modify the value of constant variable to which it points
void f( constint *xPtr )
{
y = 99;
xPtr = &y; // error: cannot modify a const object
} // end function f
Line 19 will not lead to a compiler error. You don't have a constant pointer, but a pointer to a constant object.
I.e. the pointer can be modified, but you cannot modify the object pointed to.
1 2 3 4 5
void f( constint *xPtr )
{
y = 99; // just sets the global variable
xPtr = &y; // this is ok, it just has no effect outside of the function
}
I think he just want's pass by reference. const has no place it what he's trying to do.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
void f(/*const*/ int *); // f is filling in a value, the value pointed to isn't const
int y;
int main()
{
f( &y);
cout << y << endl ;
}
void f(/*const*/ int *xPtr )
{
y = 99;
*xPtr = y;
}
And you're right, there's two things that can be const, the pointer itself and the thing pointed to. Here's the syntax:
1 2 3 4
constint* p; // the thing pointed to is const, you can change the value of p
intconst *q; // same as p
int * const r; // the pointer is const, but the thing pointed to can change value
intconst * const s; // the pointer is const and the thing pointed to is const
int main(void)
{
constint bar = 3;
constint * good_ptr = &bar;
// This would give an error:
// *good_ptr = 20;
std::cout << "The address of bar is : " << &bar << " and holds the value :" << bar << std::endl;
std::cout << "The value of good_ptr is: " << good_ptr << " and points to the value :" << *good_ptr << std::endl;
std::cout << std::endl;
int * evil_ptr = const_cast<int*>(&bar);
// Undefined behavior
*evil_ptr = 10;
std::cout << "The address of bar is : " << &bar << " and holds the value :" << bar << std::endl;
std::cout << "The value of good_ptr is: " << good_ptr << " and points to the value :" << *good_ptr << std::endl;
std::cout << "The value of evil_ptr is: " << evil_ptr << " and points to the value :" << *evil_ptr << std::endl;
return 0;
}
The address of bar is : 0036FD10 and holds the value :3
The value of good_ptr is: 0036FD10 and points to the value :3
The address of bar is : 0036FD10 and holds the value :3
The value of good_ptr is: 0036FD10 and points to the value :10
The value of evil_ptr is: 0036FD10 and points to the value :10
So general, yes you can cast away const but it is not a very good idea.
#include <iostream>
usingnamespace std;
void f( int * ); // prototype
int main()
{
int y;
f( &y);
cout << y << endl ;
system ("pause");
} // end main
void f( int *xPtr )
{
constint y = 100;
*xPtr = y;
} // end function f
#include <iostream>
usingnamespace std;
void f( int & ); // prototype
int main()
{
int y;
f( y);
cout << y << endl ;
system ("pause");
} // end main
void f( int &x )
{
constint y = 100;
x = y;
} // end function f