静的ポインタconst

4528 ワード

int *const
const int*
それらの違い、およびconst int*constの違い

// 2011 3 3
#include<iostream>
using namespace std;

int main()
{
	int b = 500,b1 = 200;
	const int *a = &b;
	cout << "&b= " << &b << endl;
	cout << "a= " << a << endl;
	cout << "*a= " << *a << endl;
	a = &b1;
    cout << "&b1= " << &b1 << endl;
	cout << "a= " << a << endl;
	cout << "*a= " << *a << endl;
	//*a = 3;
	int const *a1 = &b;
	cout << "a1= " << a1 << endl;
	a1 = &b1;
	cout << "a1= " << a1 << endl;
	//*a1 = 4; //       
	int * const c = &b;
	cout << "c= " << c << endl;
	cout << "*c= " << *c << endl;
	//c = &b1;
	*c = 4;
    cout << "c= " << c << endl;
	cout << "*c= " << *c << endl;

	const int * const d = &b;
	cout << "d= " << d << endl;
	cout << "*d= " << *d << endl;
	//d = &b1;
	//*d = 5;
	return 0;
}


一const基礎
constキーワードがポインタに関連していない場合は、ポインタに関連する場合をよく理解します.
int b = 500;
const int* a = &b; [1]
int const *a = &b;[2]
int* const a = &b;[3]
const int* const a = &b;[4]
1.constがアスタリスクの左側にある場合、constはポインタが指す変数を修飾するために使用され、すなわちポインタが定数を指す.constがアスタリスクの右側にある場合、constは修飾ポインタ自体、すなわちポインタ自体が定数です.したがって、[1]と[2]の場合と同様に、ポインタが指すコンテンツが定数である(constが変数宣言子の位置に置かれていることは関係ない)場合、*a=3ができないなど、コンテンツの変更操作は許されない.
2.[3]ポインタ自体が定数であり、ポインタが指す内容が定数ではない場合、a++が間違っているなど、ポインタ自体を変更することはできない.
3.[4]はポインタ自体と指向する内容がいずれも通常である.
以下にまとめる.
const int* a = &b;相当于:const int(*a)=&b;この静的とは、(int*a)すなわちポインタが指す変数が静的であることを指す.
int const *a = &b;相当于:int const(*a)=&b;同じ指向変数が静的である.
int* const a = &b; 相当于:int(*const a)=&b;ポインタは静的ですが、ポインタが指す変数の値は変更できます.
const int* const a = &b; ポインタとポインタが指す変数は静的です.
2011 4 25補足

#include <iostream>
using namespace std;

int main()
{
	int age = 39;
	int *ptt = &age;
         const int *pt = &age;
	*ptt += 1;
	cout << *pt  <<endl << age << endl;
	return 0;
}

出力:40
     40
const int *pt = &age;
Our declaration for pt doesn't necessarily mean that the value it points to is really a constant; it just means the value is a constant insofar as pt is concerned.
  For example, pt points to age, and age is not const. You can change the value of age directly by using the age variable, but you can't change the value indirectly via the pt pointer
つまりconst int*pt=&ageを利用している.場合、ageのcopyコピーは静的量であるが、age自体は静的量ではなく、age自体の値は変更できるが、ポインタptによってageの値を変更することはできない.ポインタptによって変更された値は静的ageコピーであるため、変更できない.
That leaves two other possibilities: assigning the address of a const variable to a pointer-to-const and assigning the address of a const to a regular pointer. Are they both possible? The first is, and the second isn't:

const float g_earth = 9.80;
const float * pe = &g_earth;   // VALID

const float g_moon = 1.63;
float * pm = &g_moon;          // INVALID

For the first case, you can use neither g_earth nor pe to change the value 9.80. C++ doesn't allow the second case for a simple reason—if you can assign the address of g_moon to pm, then you can cheat and use pm to alter the value of g_moon. That makes a mockery of g_moon's const status, so C++ prohibits you from assigning the address of a const to a non-const pointer.
What does the expression *"pizza"mean? What about "taco"[2]?
A: Because C++ interprets "pizza"as the address of its first element, applying the * operator yields the value of that first element, which is the character p. Because C++ interprets "taco"as the address of its first element, it interprets "taco"[2] as the value of the element two positions down the line, that is, as the character c. In other words, the string constant acts the same as an array name.