C++靜態(tài)成員函數(shù)和this指針詳解
靜態(tài)成員
靜態(tài)成員就是在成員變量和成員函數(shù)前加上關(guān)鍵字static,稱為靜態(tài)成員
靜態(tài)成員分為:
1.靜態(tài)成員變量
所有對象共享同一份數(shù)據(jù) 在編譯階段分配內(nèi)存 類內(nèi)聲明,類外初始化
示例:
#include<iostream>
using namespace std;
class Person
{
public:
static int m; // 所有對象共享同一份數(shù)據(jù)
};
int Person::m = 0;// 類內(nèi)聲明,類外初始化
2.靜態(tài)成員函數(shù)
所有對象共享一個函數(shù) 靜態(tài)成員函數(shù)只能訪問靜態(tài)成員變量
#include<iostream>
using namespace std;
class Person
{
public:
static void func()
{
cout << "static void func調(diào)用" << endl;
m_a = 100;//靜態(tài)成員函數(shù)可以訪問靜態(tài)成員變量
//m_b=100,靜態(tài)成員函數(shù)不可以訪問非靜態(tài)成員變量
//原因無法區(qū)分到底哪個是對象的m_b;
}
static int m_a;//靜態(tài)成員變量
int m_b;
};
int Person::m_a = 0;
int main()
{
//1.通過對象訪問
Person p;
p.func();
//2.通過類名訪問
Person::func();
system("pause");
return 0;
}
靜態(tài)成員函數(shù)可以訪問靜態(tài)成員變量
靜態(tài)成員函數(shù)不可以訪問非靜態(tài)成員變量
私有權(quán)限的靜態(tài)成員函數(shù),也是訪問不到的
成員變量和成員函數(shù)分開存儲
在C++中,類內(nèi)的成員變量和成員函數(shù)分開存儲
只有非靜態(tài)成員變量才屬于類的對象上
空對象:
#include<iostream>
using namespace std;
class Person
{
};
void test01()
{
Person p;
//空對象占用內(nèi)存空間為:1
//C++編譯器會給每個空對象也分配一個字節(jié)空間,是為了區(qū)分空對象占內(nèi)存的位置
//每個空對象也應(yīng)該有獨(dú)一無二的內(nèi)存地址
cout << sizeof(p) << endl;
}
int main()
{
test01();
return 0;
}
輸出結(jié)果:1
#include<iostream>
using namespace std;
class Person
{
int m_a;//非靜態(tài)成員變量 屬于類的對象上
};
void test02()
{
Person p;
cout << sizeof(p) << endl;
}
int main()
{
test02();
}
輸出結(jié)果:4
#include<iostream>
using namespace std;
class Person
{
int m_a;//非靜態(tài)成員變量 屬于類的對象上
static int m_b; //靜態(tài)成員變量 不屬于類的對象上
};
void test02()
{
Person p;
cout << sizeof(p) << endl;
}
int main()
{
test02();
}
輸出結(jié)果:4
與第二個對比可知:
靜態(tài)成員變量 不屬于類的對象上
#include<iostream>
using namespace std;
class Person
{
int m_a;//非靜態(tài)成員變量 屬于類的對象上
static int m_b; //靜態(tài)成員變量 不屬于類的對象上
void func() {}//非靜態(tài)成員函數(shù) 不屬于類的對象上
static void func2() {} //靜態(tài)成員函數(shù)也不會屬于 類的對象上
};
int Person::m_b = 0;
void test02()
{
Person p;
cout << sizeof(p) << endl;
}
int main()
{
test02();
}
輸出結(jié)果:4
結(jié)論:只有非靜態(tài)成員變量才屬于類的對象上
this 指針
每一個非靜態(tài)成員函數(shù)只會誕生一份函數(shù)實(shí)例,也就是說多個同類型的對象會共用一塊代碼
那么問題是:這塊代碼是如何區(qū)分是哪個對象調(diào)用自己的呢?
C++通過提供的特殊的對象指針,this指針,解決上述問題,this 指針指向被調(diào)用的成員函數(shù)所屬的對象,通俗的說,誰調(diào)用它,this就指向誰
this 指針是所有成員函數(shù)的隱含參數(shù)嗎,不需要定義,可直接使用
this 指針的用途
1.當(dāng)形參和成員變量同名時,可用this指針來區(qū)分 2.在類的非靜態(tài)成員函數(shù)中返回對象本身,可用 return *this
1.當(dāng)形參和成員變量同名時,可用this指針來區(qū)分
#include<iostream>
using namespace std;
class Person
{
public:
void func(int age)
{
this->age = age; //
}
int age;
};
int main()
{
Person p;
p.func(18);
cout << p.age << endl;
system("pause");
return 0;
}
2.在類的非靜態(tài)成員函數(shù)中返回對象本身,可用 return *this
#include<iostream>
using namespace std;
class Person
{
public:
Person& func(Person&p)
{
this->age += p.age;
return *this;
}
int age;
};
int main()
{
Person p;
p.age = 10;
//鏈?zhǔn)骄幊趟枷?
p.func(p).func(p).func(p);
cout << p.age << endl;
system("pause");
return 0;
}
空指針訪問成員函數(shù)
C++中空指針是可以調(diào)用成員函數(shù),但是也要注意有沒有用到this指針
如果用到this指針,需要加以判斷保證代碼的健壯性
#include<iostream>
using namespace std;
class Person
{
public:
void ShowPersonclass()
{
cout << "調(diào)用ShowPerclass()函數(shù)" << endl;
}
};
int main()
{
Person* p = NULL;
p->ShowPersonclass();
system("pause");
return 0;
}
通過空指針p是可以訪問到成員函數(shù)(不帶this指針的成員函數(shù))
如下代碼就是一個錯誤代碼
#include<iostream>
using namespace std;
class Person
{
public:
void ShowPersonname()
{
cout << m_name << endl; //此處出現(xiàn)了this指針
}
int m_name;
};
int main()
{
Person* p = NULL;
p->ShowPersonname();
system("pause");
return 0;
}
解析:
此處出現(xiàn)了this指針
cout << m_name << endl;
相當(dāng)于
cout <<this -> m_name << endl;
而this指針是一個空指針,所以會報錯
為了增加代碼的健壯性,我們因該做出如下改動
#include<iostream>
using namespace std;
class Person
{
public:
void ShowPersonname()
{
if (this == NULL) //在此判斷this是否是空指針
return;
cout << m_name << endl;
}
int m_name;
};
int main()
{
Person* p = NULL;
p->ShowPersonname();
system("pause");
return 0;
}
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
wxWidgets實(shí)現(xiàn)無標(biāo)題欄窗口拖動效果
這篇文章主要為大家詳細(xì)介紹了wxWidgets實(shí)現(xiàn)無標(biāo)題欄窗口拖動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02
C++實(shí)現(xiàn)簡單的希爾排序Shell Sort實(shí)例
這篇文章主要介紹了C++實(shí)現(xiàn)簡單的希爾排序Shell Sort實(shí)例,對于正在學(xué)習(xí)算法的朋友很有借鑒價值,需要的朋友可以參考下2014-07-07
Qt實(shí)現(xiàn)發(fā)送HTTP請求的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何通過Qt實(shí)現(xiàn)發(fā)送HTTP請求,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03
C語言?struct結(jié)構(gòu)體超詳細(xì)講解
C語言中,結(jié)構(gòu)體類型屬于一種構(gòu)造類型(其他的構(gòu)造類型還有:數(shù)組類型,聯(lián)合類型),下面這篇文章主要給大家介紹了關(guān)于C語言結(jié)構(gòu)體(struct)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04

