首页
社区
课程
招聘
[讨论]c++ primer 中的一点小问题,关于作用域的...
发表于: 2011-10-12 19:47 4580

[讨论]c++ primer 中的一点小问题,关于作用域的...

2011-10-12 19:47
4580
class Person{
public:
Person(const string &n,const string &add,const int &h):name(n),address(add),height(h){}
string GetName()
{
return name;
}
string GetAddress()
{
return address;
}
void xx(int height,int width)
{
cout<<this->height*width;
cout<<endl;
cout<<Person::height*width;
}
bool isequl(const Person & p)
{
Person const *pp;
cout<<typeid(pp).name()<<endl;
cout<<typeid(this).name()<<endl;
//++this;////测试用途
return name==p.name;
}
private:
string name;
string address;
int height;
};
调用:
Person yangmi("yangmi","beijing",3);
Person handsome("yangmi","shanghai",4);
cout<<yangmi.GetAddress()<<endl;
cout<<yangmi.isequl(handsome);
cout<<endl;
yangmi.xx(24,5);

问题:
cout<<this->height*width;
这样可以理解,说明用的是this所指对象(也就是调用该成员函数的对象)的height成员变量,无可厚非的用法。
cout<<Person::height*width;
这样的用法怎么解释或理解呢?
Person::height代表什么?代表height属于类内部的成员?一个类可以有多个实例,那这种调用如何体现出来此时的height成员是哪个对象的?
求解释。。。thx!!!

[课程]FART 脱壳王!加量不加价!FART作者讲授!

收藏
免费 0
支持
分享
最新回复 (7)
雪    币: 270
活跃值: (97)
能力值: ( LV8,RANK:140 )
在线值:
发帖
回帖
粉丝
2
就是跟this一样,一般很少回这样写吧,只有在子类中涉及到覆盖要用父类的变量或者函数时会这样写。
你这种写法只能在类内部使用的,就是指对象自己了。
2011-10-12 21:00
0
雪    币: 188
活跃值: (85)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
3
我一直决得在成员函数内部使用this指针指向成员变量或函数调用是一件非常不professional的做法

对于第二个做法Person::height, 当Person是基类, 而Person2是从Person继承的
在Person2的里面也有一个同名的height, 当你在Person2的成员函数里需要修改属于Person的height的时候
需要指明Person::height, 否则默认是Person2的height变量
2011-10-12 22:15
0
雪    币: 132
活跃值: (214)
能力值: ( LV6,RANK:80 )
在线值:
发帖
回帖
粉丝
4
应该是这样的:this->Person::height
就比较容易理解了。
2011-10-13 13:22
0
雪    币: 188
活跃值: (85)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
5
我的理解, this指针基本上只有在当做参数传递的时候有意义,其他时候都可以省略
2011-10-13 14:14
0
雪    币: 90
活跃值: (91)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
嘿嘿

是不是说
1.在子类中等于 父类::height
2.在本类中等于 this.height
2011-10-13 14:54
0
雪    币: 21
活跃值: (15)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
xx的函数参数中也有个height,用Person::height表示类中的height而不是参数height
2011-10-17 01:52
0
雪    币: 579
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
理解不了。这是什么?
2011-10-18 11:39
0
游客
登录 | 注册 方可回帖
返回
//