关于&和*运算符的疑惑
老师好!为什么*可以这么用:
int const *myAge = &age; int * const myAge = &age; const int * const pi2 = &age; int const * const pi2 = &age;
而&不可以这么用,但是如果这么用:
int const &myAge = age; int & const myAge = age; const int const &myAge = age; const int & const myAge = age;
vs2013下会报错但可以运行,&和*到底有什么本质上的差别(老师的课讲的真好)
#include <iostream>
using namespace std;
int main(){
int age = 22;
int yourAge = 23;
const int &myAge = age; //age的别名myAge是量量,也就是说不能通过myAge重新给age赋值
const int *pi = &age;//*pi是常量指针,不能通过*pi重新给age赋值
int * const pi1 = &age;//pi1是常量,pi1所存储的地址不能修改
//下面两行代码等价
const int * const pi2 = &age;
int const * const pi2 = &age;
cout << myAge << endl;
cout << &myAge << endl;
/*int const &myAge = age;
cout << myAge << endl;
cout << &myAge << endl;*/
/*int & const myAge = age;
cout << myAge << endl;
cout << &myAge << endl;*/
/*const int const &myAge = age;
cout << myAge << endl;
cout << &myAge << endl;*/
/*const int & const myAge = age;
cout << myAge << endl;
cout << &myAge << endl;*/
return 0;
}