C++ revisit

来源:互联网 发布:windows专业版和旗舰版 编辑:程序博客网 时间:2024/06/08 10:43

1,引用不能重新绑定,且引用不是对象,所以没有指向引用的指针

int a=1,b=2;int &c = a;int &c = b;//wrong

int a=1;int &b = a;int &*c = &b;//error: cannot declare pointer to 'int&'|

2,不能通过const 引用改变引用对象的值,但如果引用对象的值改变,const 引用的值也改变。

int a=1;const int &b = a;int &c = a;c = 2;cout<<a<<" "<<b<<" "<<c ;


3,指向常量的指针,和常量指针:

int a1=1,a2=2;const int *b = &a1;//指向常量的指针int *const c = &a1;//常量指针cout<<a1<<" "<<*b<<" "<<*c<<endl; //*b = 2;//wrongb = &a2;*c = a2;//c = &a2;//wrong 不能改变地址cout<<a1<<" "<<*b<<" "<<*c<<endl ;

4,指针相互赋值

int a=1,b=2,c=3,d=4;int *ptr1 = &a;const int *ptr2 = &b;int *const ptr3 = &c;const int* const ptr4 = &d;cout<<*ptr1<<"  "<<*ptr2<<"  "<<*ptr3<<"  "<<*ptr4<<endl;

指针相互赋值时,底层指针可以相互赋值:

ptr2 = ptr4const int* const ptr5 = ptr2;

 







原创粉丝点击