|
在C++中,靜態(tài)成員是屬于整個(gè)類的而不是某個(gè)對(duì)象,靜態(tài)成員變量只存儲(chǔ)一份供所有對(duì)象共用。所以在所有對(duì)象中都可以共享它。
使用靜態(tài)成員變量實(shí)現(xiàn)多個(gè)對(duì)象之間的數(shù)據(jù)共享不會(huì)破壞隱藏的原則,保證了安全性還可以節(jié)省內(nèi)存。
靜態(tài)成員的定義或聲明要加個(gè)關(guān)鍵static。靜態(tài)成員可以通過(guò)雙冒號(hào)來(lái)使用即<類名>::<靜態(tài)成員名>。
在C++中類的靜態(tài)成員變量和靜態(tài)成員函數(shù)是個(gè)容易出錯(cuò)的地方,本文先通過(guò)幾個(gè)例子來(lái)總結(jié)靜態(tài)成員變量和成員函數(shù)使用規(guī)則,再
給出一個(gè)實(shí)例來(lái)加深印象。希望閱讀本文可以使讀者對(duì)類的靜態(tài)成員變量和成員函數(shù)有更為深刻的認(rèn)識(shí)。
第一個(gè)例子,通過(guò)類名調(diào)用靜態(tài)成員函數(shù)和非靜態(tài)成員函數(shù)
class Point
{
public:
void init()
{
}
static void output()
{
}
};
void main()
{
Point::init();
Point::output();
}
編譯出錯(cuò):error C2352: 'Point::init' : illegal call of non-static member function
結(jié)論1:不能通過(guò)類名來(lái)調(diào)用類的非靜態(tài)成員函數(shù)。
第二個(gè)例子,通過(guò)類的對(duì)象調(diào)用靜態(tài)成員函數(shù)和非靜態(tài)成員函數(shù)
將上例的main()改為:
void main()
{
Point pt;
pt.init();
pt.output();
}
編譯通過(guò)。
結(jié)論2:類的對(duì)象可以使用靜態(tài)成員函數(shù)和非靜態(tài)成員函數(shù)。
第三個(gè)例子,在類的靜態(tài)成員函數(shù)中使用類的非靜態(tài)成員
#include <stdio.h>
class Point
{
public:
void init()
{
}
static void output()
{
printf("%d\n", m_x);
}
private:
int m_x;
};
void main()
{
Point pt;
pt.output();
}
編譯出錯(cuò):error C2597: illegal reference to data member 'Point::m_x' in a static member function
因?yàn)殪o態(tài)成員函數(shù)屬于整個(gè)類,在類實(shí)例化對(duì)象之前就已經(jīng)分配空間了,而類的非靜態(tài)成員必須在類實(shí)例化對(duì)象后才有內(nèi)存空間,所
以這個(gè)調(diào)用就出錯(cuò)了,就好比沒(méi)有聲明一個(gè)變量卻提前使用它一樣。
結(jié)論3:靜態(tài)成員函數(shù)中不能引用非靜態(tài)成員。
第四個(gè)例子,在類的非靜態(tài)成員函數(shù)中使用類的靜態(tài)成員
class Point
{
public:
void init()
{
output();
}
static void output()
{
}
};
void main()
{
Point pt;
pt.output();
}
編譯通過(guò)。
結(jié)論4:類的非靜態(tài)成員函數(shù)可以調(diào)用用靜態(tài)成員函數(shù),但反之不能。
第五個(gè)例子,使用類的靜態(tài)成員變量
#include <stdio.h>
class Point
{
public:
Point()
{
m_nPointCount++;
}
~Point()
{
m_nPointCount--;
}
static void output()
{
printf("%d\n", m_nPointCount);
}
private:
static int m_nPointCount;
};
void main()
{
Point pt;
pt.output();
}
按Ctrl+F7編譯無(wú)錯(cuò)誤,按F7生成EXE程序時(shí)報(bào)鏈接錯(cuò)誤
error LNK2001: unresolved external symbol "private: static int Point::m_nPointCount" (?m_nPointCount@Point@@0HA)
這是因?yàn)轭惖撵o態(tài)成員變量在使用前必須先初始化。
在main()函數(shù)前加上int Point::m_nPointCount = 0;
再編譯鏈接無(wú)錯(cuò)誤,運(yùn)行程序?qū)⑤敵?。
結(jié)論5:類的靜態(tài)成員變量必須先初始化再使用。
結(jié)合上面的五個(gè)例子,對(duì)類的靜態(tài)成員變量和成員函數(shù)作個(gè)總結(jié):
一。靜態(tài)成員函數(shù)中不能調(diào)用非靜態(tài)成員。
二。非靜態(tài)成員函數(shù)中可以調(diào)用靜態(tài)成員。因?yàn)殪o態(tài)成員屬于類本身,在類的對(duì)象產(chǎn)生之前就已經(jīng)存在了,所以在非靜態(tài)成員函數(shù)中
是可以調(diào)用靜態(tài)成員的。
三。靜態(tài)成員變量使用前必須先初始化(如int MyClass::m_nNumber = 0;),否則會(huì)在linker時(shí)出錯(cuò)。
再給一個(gè)利用類的靜態(tài)成員變量和函數(shù)的例子以加深理解,這個(gè)例子建立一個(gè)學(xué)生類,每個(gè)學(xué)生類的對(duì)象將組成一個(gè)雙向鏈表,用一
個(gè)靜態(tài)成員變量記錄這個(gè)雙向鏈表的表頭,一個(gè)靜態(tài)成員函數(shù)輸出這個(gè)雙向鏈表。
#include <stdio.h>
#include <string.h>
const int MAX_NAME_SIZE = 30;
class Student
{
public:
Student(char *pszName);
~Student();
public:
static void PrintfAllStudents();
private:
char m_name[MAX_NAME_SIZE];
Student *next;
Student *prev;
static Student *m_head;
};
Student::Student(char *pszName)
{
strcpy(this->m_name, pszName);
//建立雙向鏈表,新數(shù)據(jù)從鏈表頭部插入。
this->next = m_head;
this->prev = NULL;
if (m_head != NULL)
m_head->prev = this;
m_head = this;
}
Student::~Student ()//析構(gòu)過(guò)程就是節(jié)點(diǎn)的脫離過(guò)程
{
if (this == m_head) //該節(jié)點(diǎn)就是頭節(jié)點(diǎn)。
{
m_head = this->next;
}
else
{
this->prev->next = this->next;
this->next->prev = this->prev;
}
}
void Student::PrintfAllStudents()
{
for (Student *p = m_head; p != NULL; p = p->next)
printf("%s\n", p->m_name);
}
Student* Student::m_head = NULL;
void main()
{
Student studentA("AAA");
Student studentB("BBB");
Student studentC("CCC");
Student studentD("DDD");
Student student("MoreWindows");
Student::PrintfAllStudents();
}
程序?qū)⑤敵觯?br />
DDD
CCC
BBB
AAA
|
|