-
為什么int main(void)上面還要有一行void fun(int &a, int &b); ?下面定義函數(shù)的時候不是都寫了嗎?
查看全部 -
引用必須初始化
查看全部 -
首先關(guān)于&符號是取別名作用的:例子 int a = 10; int &b = a;這里對b操作就是對a操作
之后取別名&和指針類型一起。
int a=10;
int *p = &a;//設(shè)置指針p指向a的地址,定義指針時必須加*符號
int *&q = p;//為指針p設(shè)置別名
這里需要注意,定義時int *p = &a,那么之后p就是指針,*p表示的是p指針指向的地址里面存放的數(shù)
查看全部 -
void?fun(int?i,int?j=5,int?k=10); void?fun(int?i,int?j=5,int?k);//錯誤
有默認(rèn)參數(shù)值的參數(shù)必須在參數(shù)表的最右端
無實參則用默認(rèn)值,否則實參值覆蓋默認(rèn)值
查看全部 -
int main()
{
const int x=3;
x=5;//編譯錯誤,const 定義常量,不能給常量賦值。
int const *p=&x;//const int *p=&x; ? ?這兩種寫法表示意義相同。此時可以修改p所指向的內(nèi)容。如p=&y;編譯通過
*p=5;//編譯錯誤。const定義常量,不能給常量復(fù)制。
x=5;//編譯通過
int y=5;
int *const p=&x;//const定義常量。p指向了x,不能再指向其他。
p=&y;//編譯錯誤
*p=10;
cout<< x<<endl;//編譯通過,x=10當(dāng)const在*和p之間時,*p可以賦值
int const &z=x;//const與引用,z是常量,不可以改變,x可以改變
return 0;
}
查看全部 -
通過const來定義一個常量更好
查看全部 -
const?int?x=3;????int?*y=&x; //錯誤,x權(quán)限更大,*y權(quán)限較小,若如上則可通過*y改變x的值 int?x=3;????const?int?*y=&x; //正確,x有讀和寫的權(quán)限,*y只有讀的權(quán)限
查看全部 -
#include?<iostream> #include?<stdlib.h> using?namespace?std; int?main(){ //int?*p?=?new?int(20); //申請一個int空間并初始化為20 int?*p?=?new?int; //看是否申請成功,若失敗則退出 if(NULL?=?p){ system("pause"); return?0; } *p?=?20; cout<<*p<<endl; ? //使用完成之后需要釋放 ? delete?p; ? p?=?NULL; ? system("pause"); ? return?0; ? } ?
若申請塊內(nèi)存: #include?<iostream> #include?<stdlib.h> using?namespace?std; int?main(){ //申請塊空間 int?*p?=?new?int[1000]; //看是否申請成功,若失敗則退出 if(NULL?=?p){ system("pause"); return?0; } p[0]?=?10; p[1]?=?20; cout<<p[0]<<","<<p[1]<<endl; delete?[]p;//注意要加[] p?=?NULL; system("pause"); return?0; }
查看全部 -
筆記被刪了氣死
查看全部 -
c++函數(shù)新特性:
函數(shù)重載:在相同作用域內(nèi):
用同一函數(shù)名定義的多個函數(shù)
參數(shù)個數(shù)和參數(shù)類型不同
內(nèi)聯(lián)函數(shù):
有的時候3用時不長,反而2、4長,這種情況非常適合使用內(nèi)聯(lián)函數(shù)
查看全部 -
#include?<iostream> #include?<stdlib.h> using?namespace?std; //define?x?3;//不推薦,更推薦const,因為編譯器要幫忙檢查編譯錯誤 int?main(void){ ????int?x?=?3; ????int?const?*p?=?&x;//const?int?*p?=?&x;等價 ????//*p?=?5;錯誤 ????x?=?5;//正確 ????system("pause"); ????return?0; }
注意看const和*號的前后位置
const修飾引用: int?main(){ ????int?x?=?3;? ????int?y?=?5; ????int?&z?=?x; ????z?=?10; ????cout<<x<<endl;//結(jié)果正確,x=10; ???? ????system("pause"); ????return?0; } 若改成:? int?const?&z?=?x;?//則報錯
const在函數(shù)中特殊的用法,保證傳入函數(shù)的參數(shù)不會被修改。
查看全部 -
const與基本的數(shù)據(jù)類型: const?int?x?=?3;//常量x,后續(xù)不可以改變x的值;
const與指針類型: const?int?*p?=?NULL; int?const?*p?=?NULL;//完全等價 int?*const?p?=?NULL;//有區(qū)別 const可以加在兩處: const??int*?const*p?=?NULL; int?const*?const?p?=?NULL;//完全等價
int?x?=?3; const?int?*p?=?&x; //p?=?&y;正確 //*p?=?4;錯誤p指向的是常量 int?x=3; int?*const?p?=?&x; //p?=?&y;錯誤,此時的指針p是常量 const?int?x?=?3; const?int?*const?p?=?&x; //p=?&y;?*p?=?4;都是錯誤的
學(xué)習(xí)區(qū)別網(wǎng)址:https://www.cnblogs.com/xwdreamer/archive/2012/04/12/2444494.html
const與引用 int?x?=?3; const?int?&y?=?x;//y是x的別名 //x?=?10;正確 //y?=?20?;錯誤
示例:
以下都是錯誤的:
const?int?x?=?3;x?=?5; int?x?=?3;?const?int?y?=?x;?y?=?5; int?x?=?3;?const?int?*y?=?&x?;?*y?=?5; int?x?=?3;?z?=?4;?int?*const?y?=?&x;?y?=?&z; const?int?x?=?3;?const?int?&y?=?x;?y?=?5; const?int?x?=?3;?int?*y?=?&x;//通過可變的指針來指向不可變的常量,有風(fēng)險,且很大,故錯誤
而這樣是正確的:
int?x?=?3;?const?int?*y?=?&x;//指針y所指向的位置不可變,x本身具有讀和寫兩種權(quán)限,而指針只具有讀權(quán)限,用一個權(quán)限小的指針接受一個權(quán)限更大的變量,正確。
查看全部 -
待做筆記。。
查看全部 -
引用就是變量的別名。
基本數(shù)據(jù)類型的引用: #include?<iostream> using?namespace?std; int?main(){ ????int?a?=?3; ????int?&b?=?a;//引用必須初始化 ????b?=?10; ????cout<<a<<endl;//結(jié)果是10 ????return?0; }
對引用做任何操作實際在對本身進(jìn)行操作
結(jié)構(gòu)體類型的引用: typedef?struct{ ????int?x; ????int?y; }Coor; #include?<iostream> using?namespace?std; int?main(){ ????Coor?c1; ????Coor?&c?=?c1; ????c.x?=?10; ????c.y?=?20; ????cout<<c1.x<<c1.y;//結(jié)果是10、20 ????return?0; }
操作c,c1沒有區(qū)別
指針類型的引用: 類型?*&指針引用名?=?指針; #include?<iostream> using?namespace?std; int?main(){ ????int?a?=?10; ????int?*p?=?&a; ????int?*&q?=?p;//注意理解 ????*q?=?20; ????cout<<a<<endl;//結(jié)果是10 ????return?0; }
引用作函數(shù)參數(shù) c語言中用函數(shù)交換兩個值: void?fun(int?*a,int?*b){ ????int?c?=?0; ????c?=?*a; ????*a?=?*b; ????*b?=?c; } int?x?=?10,y?=?20; fun(&x,&y); c++中用引用: void?fun(int?&a,int?&b){//分別對傳進(jìn)的參數(shù)取別名 ????int?c?=?0; ????c?=?a; ????a?=?b; ????b?=?c; } int?x?=?10,y?=?20; fun(x,y);
查看全部 -
基本數(shù)據(jù)類型
const int x = 3; //常量
x變量為常量,不可被重新賦值,
指針類型
const int *p = null? <=> int const *p = null != int * const p
const int * p = &y
指針p指向的內(nèi)容被鎖定,也就是*p不能改變,
int * const p = &y
指針p被鎖定,指針不可改變
const int * const p = &y
指針p和*p被鎖定,不可以修改p的值,也不可以通過*p來修改y的值
引用
const int &y = x
y被鎖定,不可修改
查看全部
舉報