常对象与this指针

【1】示例代码

用代码说事,比较靠谱。请看下例:

 1 #include <QDebug>
 2 #include <QString>
 3 
 4 class Person
 5 {
 6 public:
 7     Person(int nAge, QString name);
 8     Person(int nAge = 18, int nSex = 1, QString name = "xiaoZhang");
 9 
10     int getAge();
11     int getAge() const;
12     QString getName();
13     int getSex();
14     int getSex() const;
15 
16 private:
17     int m_nAge;
18     int m_nSex;
19     QString m_sName;
20 };
21 
22 Person::Person(int nAge, int nSex, QString name)
23     : m_nAge(nAge)
24     , m_nSex(nSex)
25     , m_sName(name)
26 {}
27 
28 Person::Person(int nAge, QString name)
29 { // Person * const this;
30     this->m_nAge = nAge;
31     this->m_sName = name;
32     this->m_nSex = 0;
33 }
34 
35 int Person::getAge()
36 { // Person * const this
37     qDebug() << this->m_sName << "[" << this << "] call Person::getAge()";
38     return m_nAge; // this->m_nAge
39 }
40 
41 int Person::getAge() const
42 { // const Person * const this
43     qDebug() << this->m_sName << "[" << this << "] call Person::getAge() const";
44     return m_nAge; // this->m_nAge
45 }
46 
47 QString Person::getName()
48 { // Person * const this
49     qDebug() << this->m_sName << "[" << this << "] call Person::getName()";
50     return m_sName; // this->m_sName
51 }
52 
53 int Person::getSex()
54 { // Person * const this
55     qDebug() << this->m_sName << "[" << this << "] call Person::getSex()";
56     return m_nSex; // this->m_nSex
57 }
58 
59 int Person::getSex() const
60 { // const Person * const this
61     qDebug() << this->m_sName << "[" << this << "] call Person::getSex() const";
62     return m_nSex; // this->m_nSex
63 }
64 
65 void main()
66 {
67     const Person xiaoMing(20, 1, "xiaoMing");
68     Person chenPeng(22, 0, "chenPeng");
69     qDebug() << "xiaoMing :: [" << (&xiaoMing) << "]";
70     qDebug() << "chenPeng :: [" << (&chenPeng) << "]";
71 
72     xiaoMing.getAge();
73     chenPeng.getAge();
74 
75 //  xiaoMing.getName();
76 //  error error: C2662: “QString Person::getName(void)”: 不能将“this”指针从“const Person”转换为“Person &”
77     chenPeng.getName();
78 
79     xiaoMing.getSex();
80     chenPeng.getSex();
81 
82     system("pause");
83 }
84 
85 // run out:
86 /*
87 xiaoMing :: [ 0x17fdb4 ]
88 chenPeng :: [ 0x17fdc0 ]
89 "xiaoMing" [ 0x17fdb4 ] call Person::getAge() const
90 "chenPeng" [ 0x17fdc0 ] call Person::getAge()
91 "chenPeng" [ 0x17fdc0 ] call Person::getName()
92 "xiaoMing" [ 0x17fdb4 ] call Person::getSex() const
93 "chenPeng" [ 0x17fdc0 ] call Person::getSex()
94 请按任意键继续. . .
95 */

【2】常对象调用常方法

xiaoMing属于常对象。所谓常对象,即就是被const修饰的对象。对于常对象而言,其调用的方法必须是常成员方法。

所谓常成员方法,即就是由const修饰的成员方法。

在常成员方法中,其this指针被const修饰:const Person * const this;

因此,常对象必须调用常成员方法。原因如上所述。

chenPeng属于一般对象。所谓一般对象,即就是没有被const修饰的对象。对于一般对象而言,其调用的方法可以是一般成员方法,也可以是常成员方法。

所谓一般成员方法,相对于常成员方法而言,在一般成员方法中,其this指针不被const修饰:Person * const  this;

因此,一般对象既可以调用一般成员方法,也可以调用常成员方法。原因如上所述。

【3】总结

主要理解,const修饰,即具有”只读“性。

1、在C++中,this指针被隐式地声明为: T * const this; 这也就意味着不能给this指针赋值(如果这点还不理解,请参见随笔《const关键字》)

2、在T类的const成员方法中,this指针为:const T * const this; 这也就意味着this指针所指向的这个对象是不可修改的(即不能对这个对象的数据成员进行赋值操作)。

3、若成员方法被const修饰,那么成员方法内部无法修改任何成员变量的值。

4、若对象被const修饰,那么对象的属性值也不可被修改。

5、常对象只能调用常成员方法。

6、一般对象可以调用全部成员方法。

7、当一个对象调用成员方法时,编译程序先将对象的地址赋给this指针,然后调用成员方法。每次成员方法存取数据成员时,都隐含使用this指针。

8、当一个成员方法被调用时,自动向它传递一个隐含的参数,该参数是一个调用这个成员方法的对象的指针。

9、之所以this指针被const修饰,因为在每次调用的整个过程this指向都不允许改变(原因很简单,如果改变的话就不是这个对象调用的了,后果无法预料。) 

 

Good Good Study,Day Day Up.

顺序 选择 循环 总结

posted @ 2017-08-29 17:19  kaizenly  阅读(600)  评论(0编辑  收藏  举报
打赏