-
为什么int main(void)上面还要有一行void fun(int &a, int &b); ?下面定义函数的时候不是都写了吗?
查看全部 -
引用必须初始化
查看全部 -
首先关于&符号是取别名作用的:例子 int a = 10; int &b = a;这里对b操作就是对a操作
之后取别名&和指针类型一起。
int a=10;
int *p = &a;//设置指针p指向a的地址,定义指针时必须加*符号
int *&q = p;//为指针p设置别名
这里需要注意,定义时int *p = &a,那么之后p就是指针,*p表示的是p指针指向的地址里面存放的数
查看全部 -
void fun(int i,int j=5,int k=10); void fun(int i,int j=5,int k);//错误
有默认参数值的参数必须在参数表的最右端
无实参则用默认值,否则实参值覆盖默认值
查看全部 -
int main()
{
const int x=3;
x=5;//编译错误,const 定义常量,不能给常量赋值。
int const *p=&x;//const int *p=&x; 这两种写法表示意义相同。此时可以修改p所指向的内容。如p=&y;编译通过
*p=5;//编译错误。const定义常量,不能给常量复制。
x=5;//编译通过
int y=5;
int *const p=&x;//const定义常量。p指向了x,不能再指向其他。
p=&y;//编译错误
*p=10;
cout<< x<<endl;//编译通过,x=10当const在*和p之间时,*p可以赋值
int const &z=x;//const与引用,z是常量,不可以改变,x可以改变
return 0;
}
查看全部 -
通过const来定义一个常量更好
查看全部 -
const int x=3; int *y=&x; //错误,x权限更大,*y权限较小,若如上则可通过*y改变x的值 int x=3; const int *y=&x; //正确,x有读和写的权限,*y只有读的权限
查看全部 -
#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; }若申请块内存: #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++函数新特性:


函数重载:在相同作用域内:
用同一函数名定义的多个函数
参数个数和参数类型不同


内联函数:

有的时候3用时不长,反而2、4长,这种情况非常适合使用内联函数



查看全部 -
#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;//结果正确,x=10; system("pause"); return 0; } 若改成: int const &z = x; //则报错const在函数中特殊的用法,保证传入函数的参数不会被修改。
查看全部 -
const与基本的数据类型: const int x = 3;//常量x,后续不可以改变x的值;
const与指针类型: const int *p = NULL; int const *p = NULL;//完全等价 int *const p = NULL;//有区别 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;都是错误的
学习区别网址: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;//通过可变的指针来指向不可变的常量,有风险,且很大,故错误
而这样是正确的:
int x = 3; const int *y = &x;//指针y所指向的位置不可变,x本身具有读和写两种权限,而指针只具有读权限,用一个权限小的指针接受一个权限更大的变量,正确。
查看全部 -
待做笔记。。
查看全部 -
引用就是变量的别名。
基本数据类型的引用: #include <iostream> using namespace std; int main(){ int a = 3; int &b = a;//引用必须初始化 b = 10; cout<<a<<endl;//结果是10 return 0; }对引用做任何操作实际在对本身进行操作
结构体类型的引用: 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;//结果是10、20 return 0; }操作c,c1没有区别
指针类型的引用: 类型 *&指针引用名 = 指针; #include <iostream> using namespace std; int main(){ int a = 10; int *p = &a; int *&q = p;//注意理解 *q = 20; cout<<a<<endl;//结果是10 return 0; }引用作函数参数 c语言中用函数交换两个值: 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){//分别对传进的参数取别名 int c = 0; c = a; a = b; b = c; } int x = 10,y = 20; fun(x,y);查看全部 -
基本数据类型
const int x = 3; //常量
x变量为常量,不可被重新赋值,
指针类型
const int *p = null <=> int const *p = null != int * const p
const int * p = &y
指针p指向的内容被锁定,也就是*p不能改变,
int * const p = &y
指针p被锁定,指针不可改变
const int * const p = &y
指针p和*p被锁定,不可以修改p的值,也不可以通过*p来修改y的值
引用
const int &y = x
y被锁定,不可修改
查看全部
举报