auto参数,申明时可以忽略auto
void fun(){int p = 1;cout<<"p:"<<p<<endl;for(int i=0;i<=5;i++){int p =10;cout<<"p:"<<p<<endl;}// 此处输出的还是外围作用域定义的p值1cout<<"p:"<<p<<endl; }
输出如下:
#include <iostream>using namespace std;//============================================================================ //总结: //============================================================================void autoParam(){int p = 1;cout<<"p:"<<p<<endl;for(int i=0;i<=5;i++){int p =10;cout<<"p:"<<p<<endl;}// 此处输出的还是外围作用域定义的p值1cout<<"p:"<<p<<endl; }static int aa = 100; int staticParam(int x){static int n = 0;n += x;return n; }int main(){cout << "函数参数作用域实践:" << endl;autoParam();int sum = 0;for(int j = 0; j<=10;j++){sum = staticParam(j);cout<<"sum:"<<sum<<endl;}cout<<"aa:"<<aa<<endl;cout << "end." << endl;return 0; }
输出结果如下: