-
const(控制变量是否可以变化) const int x=3;(则此时x为常量,不可进行再赋值) const与指针类型 const int *p=NULL; int const *p=NULL;(两种写法完全等价) int *const p=NULL; const int *const p=NULL; int const *const p=NULL;(这两种写法也是完全等价的) int x=3; const int *p=&x; *p=4(错误,因为const指定的为*p);p=&y;(正确) int x=3; const int *const p=&x; p=&y(错误,因为const指向的为p,只能为x的地址) const与引用 int x=3;const int &y=x; y=10(错误,y通过const限定只能为x的别名,值为3) 总结: const int x=3;int *y=&x;(这种写法是错误的因为x本身定义为const,在用一个可变的指针指向,那么就有用指针改变x值得风险,这是系统所不允许的); int x=3; const int *y=&x;(正确,这样保证了指针对x只有可读性,而没有可写性) const 常量: const int*p=NULL;//建议是这样写比较方便直观 int*const y=&x时,由于const在y跟指针中间所以y如果指向了x就不能再改变了。 int x=3; const int*y=&x;相当于以权限小的接收大,这是可以的。查看全部
-
#include <iostream> using namespace std; int main(void) { int x = 3; //定义引用,y是x的引用 int &y=x; //打印x和y的值 cout<<x<<endl<<y<<endl; //修改y的值 y = 20; //再次打印x和y的值 cout<<x<<endl<<y<<endl; return 0; }查看全部
-
引用不能单独存在。引用的用法: A:int a; int &b = a; C:int *p; int *&q = p; D:int a; int &b = a; int &c = a;查看全部
-
函数引用:交换两个数 #include <iostream> using namespace std; void fun(int &a,int &b); int main() { int x=10; int y=20; cout<<x<<","<<y<<endl; fun(x,y); cout<<x<<","<<endl; } void fun(int &a,int &b) { int c=0; c=a; a=b; b=c; }查看全部
-
指针引用 #include <iostream> #include <stdlib.h> using namespace std; int main() { int a=3; int *p=&a; //指针p指向a int *&q=p; //给p取别名q *q=5; cout<<a<<endl; return 0; }查看全部
-
#include <iostream> #include <stdlib.h> using namespace std; typedef struct { int x; int y; }Coord; int main() { Coord c; Coord &c1=c; c1.x=10; c1.y=20; cout<<c.x<<","<<c.y<<endl; return 0; }查看全部
-
引用就是起别名,&a=b;即a是b的别名。 不论改变本身还是别名,等会影响其存值。 *q = &a;同理 #include <iostream> #include <stdlib.h> using namespace std; int main() { int a=10; int &b=a; b=20; cout<<a<<endl; a=30; cout<<b<<endl; return 0; }查看全部
-
引用作为函数参数:实现交换两个数查看全部
-
指针类型的引用:类型 *&指针引用名=指针;对别名的操作就是对原变量的操作查看全部
-
结构体类型的引用查看全部
-
引用就是指变量的一个别名(不能只有别名) 引用必须初始化。 int a=3; int &b=a;(为a起个别名b,也是将别名b初始化为a) b=10;(对别名做任何操作都是对其本身做操作) 结构体类型的相关引用 struct Coor{ int x,y; } Coor c1; Coor &c=c1; c.x=10; c.y=20; 指针类型的引用:类型*&指针引用名=指针; int a=10; int *p=&a; int *&q=p; *q=20;(则a的值为20) 引用做函数参数 void fun(int &a,int &b) { } fun(x,y)将x,y分别起别名,接下来在函数体中直接用别名来操作 变量引用,结构体引用,指针类型引用。 int *p = &a; int *&q = p; *q = 20; 引用作函数参数.查看全部
-
指针和引用,const, 函数默认值&函数重载。内存管理!!! C++离岗篇:引用vs指针、define VS const、 函数默认值&函数重载、内存管理查看全部
-
const此时指向的是x的别名y,y不能修改,x不受限【const与引用】查看全部
-
const此时指向的是*p,*p不能再进行赋值操作;但是p不受限【const与指针类型】查看全部
-
这样才等同查看全部
举报
0/150
提交
取消