一、靜態(tài)成員函數(shù)簡(jiǎn)介
1、靜態(tài)成員函數(shù)概念
靜態(tài)成員函數(shù)歸屬 : 在 C++ 類中 , 靜態(tài)成員函數(shù) 是一種 特殊的函數(shù) , 該函數(shù)屬于類 , 而不是屬于 類實(shí)例對(duì)象 ;
靜態(tài)成員函數(shù)調(diào)用不依賴于對(duì)象 : 即使 沒(méi)有創(chuàng)建 類 的 實(shí)例對(duì)象 , 也可以 通過(guò) 類名:: 調(diào)用 類中定義的 靜態(tài)成員函數(shù) ;
靜態(tài)成員函數(shù)作用 : 靜態(tài)成員函數(shù) 通常用于 執(zhí)行與類本身相關(guān)的操作 , 執(zhí)行該函數(shù) 不涉及到 類實(shí)例對(duì)象中的信息 , 也不能在 靜態(tài)成員函數(shù) 中訪問(wèn) 普通的 成員變量 和 成員函數(shù) ;
2、靜態(tài)成員函數(shù)聲明
靜態(tài)成員函數(shù)聲明 : 使用 static 關(guān)鍵字 修飾 成員函數(shù) , 就可以將 普通的成員函數(shù) 轉(zhuǎn)為 靜態(tài)成員函數(shù) ;
如 : 下面的 void fun() 普通成員函數(shù) , 在 函數(shù)之前添加 static 關(guān)鍵字 , static void fun() 就變成了 靜態(tài)成員函數(shù) ;
class Student
{
public:
static void fun()
{
cout << "靜態(tài)成員函數(shù)被調(diào)用 : number = " << number << endl;
}
};
3、靜態(tài)成員函數(shù)訪問(wèn)
靜態(tài)成員函數(shù)訪問(wèn) :
- 使用 類名 和 域操作符 訪問(wèn) :
// 通過(guò) 類名:: 調(diào)用 靜態(tài)成員函數(shù)
Student::fun();
- 使用 對(duì)象 訪問(wèn) :
// 通過(guò) 對(duì)象. 調(diào)用 靜態(tài)成員函數(shù)
s.fun();
4、靜態(tài)成員函數(shù)只能訪問(wèn)靜態(tài)成員
靜態(tài)成員函數(shù)內(nèi)容要求 : 靜態(tài)成員函數(shù) 只能訪問(wèn)
- 靜態(tài)成員變量
- 其他靜態(tài)成員函數(shù)
靜態(tài)成員函數(shù) 不能訪問(wèn) 非靜態(tài)成員變量 或 非靜態(tài)成員函數(shù) ,
普通的 成員變量 和 成員函數(shù) , 需要 通過(guò) 類 的 實(shí)例對(duì)象 來(lái)訪問(wèn) , 需要 依托于 對(duì)象才能存在 ,
而 靜態(tài)成員函數(shù) 可以在 不創(chuàng)建 實(shí)例對(duì)象的前提下被調(diào)用 , 因此 靜態(tài)成員函數(shù)中 不能訪問(wèn) 非靜態(tài)成員 ;
如果在靜態(tài)成員函數(shù)中 , 訪問(wèn)非靜態(tài)成員 , 會(huì)報(bào)如下錯(cuò)誤 :
1>------ 已啟動(dòng)生成: 項(xiàng)目: HelloWorld, 配置: Debug Win32 ------
1>Hello.cpp
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(22,3): error C2597: 對(duì)非靜態(tài)成員“Student::m_age”的非法引用
1>已完成生成項(xiàng)目“HelloWorld.vcxproj”的操作 - 失敗。
========== 生成: 成功 0 個(gè),失敗 1 個(gè),最新 0 個(gè),跳過(guò) 0 個(gè) ==========
二、代碼示例 - 靜態(tài)成員函數(shù)
下面的代碼 , 是在 之前 的 靜態(tài)成員變量 示例代碼的基礎(chǔ)上 進(jìn)行的 ;
在 Student 類中定義靜態(tài)成員函數(shù) , 使用 static 關(guān)鍵字修飾函數(shù) ;
注意 : 不要在 靜態(tài)函數(shù)中 訪問(wèn) 非靜態(tài)成員 , 否則會(huì)報(bào)錯(cuò) " 對(duì)非靜態(tài)成員“Student::m_age”的非法引用 " ;
static void fun()
{
// 靜態(tài)成員函數(shù) 中 訪問(wèn)非靜態(tài)成員會(huì)報(bào)錯(cuò)
// error C2597: 對(duì)非靜態(tài)成員“Student::m_age”的非法引用
//m_age = 10;
cout << "靜態(tài)成員函數(shù)被調(diào)用 : number = " << number << endl;
}
訪問(wèn) 類的 靜態(tài)成員函數(shù) 時(shí) , 可以使用 類名:: 調(diào)用 靜態(tài)成員函數(shù) ;
// 通過(guò) 類名:: 調(diào)用 靜態(tài)成員函數(shù)
Student::fun();
還可以使用 對(duì)象. 調(diào)用 靜態(tài)成員函數(shù) ;
// 通過(guò) 對(duì)象. 調(diào)用 靜態(tài)成員函數(shù)
s.fun();
代碼示例 :
#include "iostream"
using namespace std;
class Student
{
public:
// 帶參構(gòu)造函數(shù)
Student(int age, int height)
{
m_age = age;
m_height = height;
cout << "執(zhí)行 Student 的構(gòu)造函數(shù)" << endl;
}
~Student()
{
cout << "執(zhí)行 Student 的析構(gòu)函數(shù)" << endl;
}
static void fun()
{
// 靜態(tài)成員函數(shù) 中 訪問(wèn)非靜態(tài)成員會(huì)報(bào)錯(cuò)
// error C2597: 對(duì)非靜態(tài)成員“Student::m_age”的非法引用
//m_age = 10;
cout << "靜態(tài)成員函數(shù)被調(diào)用 : number = " << number << endl;
}
public:
int m_age; // 年齡
int m_height; // 身高
// 在類內(nèi)部定義靜態(tài)成員
static int number;
};
// 在類外部初始化靜態(tài)成員變量
int Student::number = 1;
int main() {
// I. 靜態(tài)成員變量
// 使用 域操作符 訪問(wèn) 類靜態(tài)成員變量
// 類名::靜態(tài)成員變量名
cout << "Student::number = " << Student::number << endl;
// 在函數(shù)中為 類 靜態(tài)成員變量 賦值
Student::number = 2;
// 創(chuàng)建 Student 類型對(duì)象
Student s(10, 150);
// 使用 對(duì)象 訪問(wèn) 類靜態(tài)成員變量
// 對(duì)象名稱.靜態(tài)成員變量名
cout << "s.number = " << s.number << endl;
// II. 靜態(tài)成員函數(shù)
// 通過(guò) 類名:: 調(diào)用 靜態(tài)成員函數(shù)
Student::fun();
// 通過(guò) 對(duì)象. 調(diào)用 靜態(tài)成員函數(shù)
s.fun();
// 控制臺(tái)暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
}
執(zhí)行結(jié)果 :文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-810623.html
Student::number = 1
執(zhí)行 Student 的構(gòu)造函數(shù)
s.number = 2
靜態(tài)成員函數(shù)被調(diào)用 : number = 2
靜態(tài)成員函數(shù)被調(diào)用 : number = 2
請(qǐng)按任意鍵繼續(xù). . .
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-810623.html
到了這里,關(guān)于【C++】靜態(tài)成員函數(shù) ( 靜態(tài)成員函數(shù)概念 | 靜態(tài)成員函數(shù)聲明 | 靜態(tài)成員函數(shù)訪問(wèn) | 靜態(tài)成員函數(shù)只能訪問(wèn)靜態(tài)成員 )的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!