?? int & != int
?? ////引用是一種類型
?? const int ==int
///const 修飾 在非引用 和 指針 情況下? 不改變類型
?? const int &? != int &
?? const int *?? !=int *???////只有 const 類型 指針能接受 const 類型 地址
??非引用類型 必須精確匹配
???float ?i=2.22f;
?? int & p= i;///error
?const int & p=i;//ok,零時對象,也就是說其可以接受 個右值或 不匹配類型
?? 特別需要注意 非const引用必需 是個精確匹配的左值
如?
?? const int a=100;
?? const int * &?p=& a//error,& a是 const int * ,?但其地址是個右值,只有 const 才能接受右植
?? const int * const & p= &a//ok;不是對“const”的引用不能綁定到非 lvalue
? int a=10;
int *& b=&a///錯誤
int *const & b=&a;//ok
但 char * a="good";//"good" 是const char [5]類型---->const char* 但? 是 a是char* ;這個是編譯器的例外
?
引用:沒有指針語法的指針,非const引用只能接受左值
指針的引用相當于一個指針的指針,常用于函數參數中來改變指針本身的值(外部同時也改變)
如
?????? void f(int *&p)
{
p=new int [5];//外部也改變了
}
?????????????void f(int **p)
{
*p=new int [5];//外部也改變
}
??????????????? void f(int * p)
{
p=new int [5];//外部不改變
}
非const 指針都可以給void *;
void *p =(int) 0x3444;
posted on 2006-05-27 14:21
黃大仙 閱讀(1209)
評論(1) 編輯 收藏 引用 所屬分類:
c++