常量指针、指针常量、引用返回静态变量
1 #include <iostream> 2 using namespace std; 3 4 //常量指针 5 void test01(){ 6 int a = 4; 7 int b = 2; 8 int *const p = &a; 9 cout<<"*p = "<<*p<<endl; 10 //p = &b;//报错,指针常量说明不可以更改指针指向的地址 11 cout<<"*p = "<<*p<<endl; 12 13 const int *q = &b; 14 //*q = 3;//报错,常量指针不可以更改指针所指向的值 15 } 16 17 //静态变量 18 int& test02(){ 19 int c = 10; 20 //函数结束c的值消失 21 static int d = 10;//函数结束不会消失 22 return d; 23 } 24 25 int main() { 26 int a = 4; 27 int b = 2; 28 //test01(); 29 30 //test02(); 31 //cout<<"d = "<<d<<endl;这个拿不到d 32 33 //通过返回引用可以拿到 34 cout<<"d = "<<test02()<<endl; 35 return 0; 36 }
主要是给自己看的,所以肯定会出现很多错误哈哈哈哈哈