首先看一段代码!!!
#include <iostream>
using namespace std;
int n = 10,m = 77777;
struct test{
int n = 9;
void print(int n){
cout << this->n << endl;
cout << n << endl;
cout << m << endl;
}
};
test t;
void print(int n,test t){
cout << n <<endl;
n = 30;
cout << n <<endl;
int a = t.n;
cout << a <<endl;
t.print(6666);
return;
}
int main(){
print(33333,t);
cout << n;
return 0;
}
结果为

可以看出
1、如果全局变量作为函数的参数传递,那么这个函数会会略全局变量
2、this.n用于结构体或者类,指向当前类的那个变量,因此在结构体或者类里面的方法的形参是有冲突的话,可以用this.n指向方法外的那个变量,这就是我混合理解倒置误会的原因。
本文通过一段C++代码实例,详细解析了结构体成员变量与全局变量的作用域,以及this指针在解决变量名冲突时的应用。强调了全局变量作为函数参数时的特殊行为,以及this.n在结构体内指向特定变量的重要性。

被折叠的 条评论
为什么被折叠?



