作者BloodyDuke (個巴樂)
看板EE_DSnP
標題Re: [問題]有關void f(int const*q)
時間Tue Nov 11 20:03:01 2008
※ 引述《bonoshi (sbo)》之銘言:
: 以下的測試我覺得有點偏了
: 會做測試的原因應該是對我先前的問題蠻好奇的才會做這些實驗
: 貼給大家參考
: 可直接copy到compiler上
: 我有都註解
: 如果有誤請高手指正
: 謝謝~
: 測試一
: #include<iostream>
: using namespace std;
: void f(int const**q);//q is a pointer to a pointer to const int
: int main(void){
: /*const*/ int a = 20;//有沒有const都沒關係
: int const* p = &a;
: f(&p);
: system("pause");
: }
: void f(int const** q){
: *q = 0;//可改*q的值但不可改**q
: }
: //有沒有const都沒關係,
: //我的解讀是只要int const * p = &a保證*p不能被修改就足夠了,
: //系統就會承認q是一個pointer to a pointer to a const int,
: //與a宣告時有沒有const無關。
: 測試二
: #include<iostream>
: using namespace std;
: void f(int *const *q);
: int main(void){
: int a = 20;
: int* p = &a;
: f(&p);
: cout << a << endl;
: system("pause");
: }
: void f(int *const *q){
: **q = 0;//雖然q是一個pointer to constant pointer to int,*q不能被修改
: ,但**q可以
: }
我做了以下的試驗
1.
int main(){
int a=1;
int *p =&a ;
const int ** q=&p;
system("pause");
}
這個由之前的討論知道不能過
2.
int main(){
int a=1;
int *p =&a ;
const int *const* q=&p;
system("pause");
}
//q is a pointer to a constant pointer pointing to a const int
這樣竟然可以過了
覺得很奇怪
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.240.243