同一般的名字查找流程,现在类内找,没找到再去外层找
typedef double Money;string bal;class Account{public : Money balance() {return bal;}private : Money bal; // ...};先编译Acount类内的成员 bal, bal的类型是Money,现在类内寻找Money的声明,没有找到,则去类外找,找到了typedef double Money。
编译完成员后编译函数体return bal;,现在类内寻找bal,找到Money bal,所以此时的返回的是Money bal而不是string bal。
int height = 1;class text{public : void h(int height){ cout<<height; //输出的是参数height }private : int height = 3;};int main(){ text t; t.h(2); //输出:2 return 0;}#include<iostream>using namespace std;int height = 1;class text{public : void h(int ht){ cout<<height; //输出的是类成员height }private : int height = 3;};int main(){ text t; t.h(2); //输出:3 return 0;}#include<iostream>using namespace std;int height = 1;class text{public : void h(int ht){ cout<<height; //输出的是全局变量height }private : int HT = 3;};int main(){ text t; t.h(2); //输出:1 return 0;}成员参数的参数,类成员以及全局变量的名词尽量不要相同