P
pauldepstein
I wrote the following code in an attempt to understand the type
conversion in the subject title:
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
int ci = 0;
const int *p = &ci;
cout << p << endl;
ci = 1;
ci = 2;
cout << "pointer_type"<< typeid(p).name()<< endl
<< "ci_value" << ci << endl << p << endl << *p
<< "ok, well if p is a const int* why id you allow me to change
ci????";
cin.get();
}
My question is apparent. I was surprised by the result: I expected
some type of error message.
Since p is a pointer_to_const, why is it ok to change the value lying
in the address that p points to? Doesn't that contradict the whole
pointer_to_const concept? (I had the (apparently false) belief that the
const int* p definition-and-declaration would convert ci to a const.)
Thank you for your explanation.
Paul Epstein
conversion in the subject title:
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
int ci = 0;
const int *p = &ci;
cout << p << endl;
ci = 1;
ci = 2;
cout << "pointer_type"<< typeid(p).name()<< endl
<< "ci_value" << ci << endl << p << endl << *p
<< "ok, well if p is a const int* why id you allow me to change
ci????";
cin.get();
}
My question is apparent. I was surprised by the result: I expected
some type of error message.
Since p is a pointer_to_const, why is it ok to change the value lying
in the address that p points to? Doesn't that contradict the whole
pointer_to_const concept? (I had the (apparently false) belief that the
const int* p definition-and-declaration would convert ci to a const.)
Thank you for your explanation.
Paul Epstein