目錄
1 成員變量與成員函數(shù)分開存儲(chǔ)
2 this指針
2.1 作用1
2.1.1示例
2.2 作用2
3 空指針訪問成員函數(shù)
4 const修飾成員函數(shù)
4.1示例
4.2 常對(duì)象
1 成員變量與成員函數(shù)分開存儲(chǔ)
在C++中,類內(nèi)的成員變量和成員函數(shù)分開存儲(chǔ)
首先,對(duì)于一個(gè)空對(duì)象,占用內(nèi)存空間為1
?class person
?{
??
?};
??
?void test01()
?{
? person p;
? cout << sizeof(p) << endl;
?}
因?yàn)镃++編譯器給每個(gè)空對(duì)象分配1個(gè)字節(jié)空間,防止不同空對(duì)象占用同一塊內(nèi)存空間,便于區(qū)分
只有非靜態(tài)成員變量才屬于類的對(duì)象上
在person類中加入一個(gè)非靜態(tài)成員變量
?class person
?{
? int m_a; // 非靜態(tài)成員變量,屬于類的對(duì)象上
?};
??
?void test02()
?{
? person p;
? cout << sizeof(p) << endl;
?}
再加上靜態(tài)成員變量
再加上非靜態(tài)成員函數(shù)
最后加上靜態(tài)成員函數(shù)
總結(jié):只有非靜態(tài)成員變量才屬于類的對(duì)象上
2 this指針
概念:上一節(jié)中我們知道成員變量與成員函數(shù)分開存儲(chǔ),每一個(gè)非靜態(tài)成員函數(shù)只會(huì)產(chǎn)生一份函數(shù)實(shí)例,也就是多個(gè)同類型的對(duì)象共用一塊代碼,而如何區(qū)分究竟是哪個(gè)對(duì)象調(diào)用自己,用到this指針
this指針指向被調(diào)用的成員函數(shù)所屬的對(duì)象
性質(zhì):
-
this
指針是隱含在每一個(gè)非靜態(tài)成員函數(shù)內(nèi)的一種指針 -
this
指針不需要定義,可以直接使用
2.1 作用1
當(dāng)形參與成員變量同名時(shí),可用this指針加以區(qū)分
2.1.1示例
我們先創(chuàng)建類person
和測(cè)試函數(shù)test01
,在類內(nèi)創(chuàng)建屬性m_age
,函數(shù)person
,形參age
?class person
?{
?public:
? person(int age)
? {
? m_age = age;
? }
? int m_age;
?};
?void test01()
?{
? person p(20);
? cout << p.m_age << endl;
?}
此時(shí)可正常輸出
但是,如果我們將屬性m_age
改為age
得到這個(gè)
?class person
?{
?public:
? person(int age)
? {
? age = age;
? }
? int age;
?};
?void test01()
?{
? person p(20);
? cout << p.age << endl;
?}
同顏色的即為同一個(gè)age
,無(wú)法正常輸出
在此基礎(chǔ)上,我們?cè)诤瘮?shù)內(nèi)加上this指針
?class person
?{
?public:
? person(int age)
? {
? this->age = age; // 加上this->
? }
? int age;
?};
可以發(fā)現(xiàn)3個(gè)age是同源的。此時(shí),this
就代表p
,this->age == p.age;
2.2 作用2
在類的非靜態(tài)成員函數(shù)中返回對(duì)象本身,可以用return *this;
?class person
?{
?public:
? person(int age)
? {
? this->age = age;
? }
? void personAdd(person& p) // 函數(shù)本體age加上參數(shù)P的age
? {
? this->age += p.age;
? }
? int age;
?};
?void test02()
?{
? person p1(10);
? cout << p1.age << endl;
? person p2(10);
? cout << p2.age << endl;
?}
而如果我們想要多次加和
?void test02()
?{
? p2.personAdd(p1);
? cout << p2.age << endl;
??
? p2.personAdd(p1).personAdd(p1).personAdd(p1); // 想要多次加和
? cout << p2.age << endl;
?}
要先修改函數(shù)體返回值
?person& personAdd(person& p) // 而且要引用
? {
? this->age += p.age;
? return *this; // 返回對(duì)象本身
? }
而如果函數(shù)體返回值不使用引用&
,則無(wú)法修改值
因?yàn)檫@樣修改完第一次p2
后,返回的是另一個(gè)變量,并且后面第2次開始修改的都是另一個(gè)變量,再另一個(gè)變量
3 空指針訪問成員函數(shù)
C++
中空指針也可以調(diào)用成員函數(shù),不過要注意是否用到this
指針
如果遇到this
指針,加上判斷,防止代碼出錯(cuò)
?class person
?{
?public:
? void show_name()
? {
? cout << "Joyce" << endl;
? }
? void show_age()
? {
? cout << this->m_age << endl;
? }
??
? int m_age;
??
?};
?void test01()
?{
? person* p = NULL;
? p->show_age(); // 非法訪問
? p->show_name();
?}
對(duì)于這段代碼,指針p為NULL
,調(diào)用show_name
函數(shù)時(shí)可以正常輸出,因?yàn)樗皇呛?jiǎn)單的打印特定的內(nèi)容,而調(diào)用show_age
函數(shù)時(shí)將報(bào)錯(cuò),因?yàn)?strong>函數(shù)內(nèi)this->m_age
;調(diào)用了this
指針的m_age
,而this
指針為NULL
,造成非法訪問
因此,我們?cè)诤瘮?shù)內(nèi)部加上判斷
?void show_age()
? {
? if (this == NULL)
? {
? return;
? }
? cout << this->m_age << endl;
? }
以此便可以防止代碼為NULL
時(shí)出錯(cuò)
4 const修飾成員函數(shù)
常函數(shù):
-
成員函數(shù)后加
const
稱該函數(shù)為成員函數(shù) -
常函數(shù)不可以修改成員屬性
-
成員屬性聲明時(shí)加關(guān)鍵字
mutable
,在常函數(shù)中仍然能修改
4.1示例
接下來(lái)創(chuàng)建person
類與函數(shù)show_name;
?class person
?{
?public:
? void show_name()
? {
? this->m_a = 100; // 非 常對(duì)象,可修改
? this = NULL;// this指針的指向不可修改
? }
? int m_a;
?};
因?yàn)?code>this指針本質(zhì)是一個(gè)指針常量,所以其指向(地址)不可修改,但其指向的值可以修改
本質(zhì):
person * const this;
不過,如果在函數(shù)后加上const
,則地址與值都不可修改
本質(zhì):
const person \*const this;
不過,如果在常函數(shù)中的屬性的聲明前加上mutable
,則可以修改
?class person
?{
?public:
? void show_name() const // 這里有const
? {
? this->m_a = 100; //可修改
? }
? mutable int m_a; // 加上const
?};
4.2 常對(duì)象
-
聲明對(duì)象前加const稱該對(duì)象為常對(duì)象
-
常對(duì)象只能調(diào)用常函數(shù)
?class person
?{
?public:
? void show_name() const
? {
? this->m_a = 100;
? }
? mutable int m_a;
? int m_b;
?};
?void test02()
?{
? person p;
? p.m_b = 20;
?}
此時(shí)test02
內(nèi)可以修改m_b;
接下來(lái)在p
前加上const
使其變成常對(duì)象
?void test02()
?{
? const person p;
? p.m_b = 20; // 不可修改
? p.m_a = 20; // 可修改
?}
則m_b
不可修改,而m_a
由于前面加了mutable
,可以修改
同時(shí),在person
類中我們?cè)賱?chuàng)建一個(gè)普通函數(shù)func
,嘗試在test
函數(shù)中調(diào)用剛才的常函數(shù)和普通函數(shù)
?class person
?{
?public:
? void show_name() const // 常函數(shù)
? {
? this->m_a = 100;
? }
? void func() // 普通函數(shù)
? {
? ;
? }
? mutable int m_a;
? int m_b;
?};
??
?void test02()
?{
? ? ?const person p; // 創(chuàng)建常對(duì)象
? p.show_name(); // 常函數(shù)
? p.func(); // 普通函數(shù)
?}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-406193.html
無(wú)法調(diào)用,因?yàn)槌?duì)象只能調(diào)用常函數(shù)(因?yàn)槠胀ê瘮?shù)可以修改屬性)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-406193.html
到了這里,關(guān)于C++對(duì)象模型與this指針的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!