C 類中的靜態成員

2021-10-05 08:00:43 字數 1179 閱讀 9261

1 靜態類成員變數

#ifndef cl_student_h

#define cl_student_h

#include

class

cl_student

;#endif

#include

qstring cl_student::sta_school =

"uestc"

;//初始化靜態類成員變數

cl_student::

cl_student

(qstring name)

cl_student::

~cl_student()

2 靜態類成員函式

#ifndef cl_student_h

#define cl_student_h

#include

class

cl_student

;#endif

#include

qstring cl_student::sta_school =

"uestc"

;//初始化靜態類成員變數

qstring cl_student::

getschool()

//靜態類成員函式的實現

cl_student::

cl_student

(qstring name)

cl_student::

~cl_student()

#include

#include

intmain()

3 靜態類成員常量

#ifndef cl_student_h

#define cl_student_h

#include

class

cl_student

;#endif

#include

qstring cl_student::sta_school =

"uestc"

;//初始化靜態類成員變數

cl_student::

cl_student

(qstring name)

cl_student::

~cl_student()

C 類中的靜態成員

相關規則 1 靜態函式不能呼叫類的成員變數和成員函式 2 成員函式可以呼叫靜態變數和靜態函式 3 靜態變數和靜態函式不屬於類的任何具體物件 沒有this指標 但是可被類的所有物件共享。4 類的靜態變數和靜態函式不能被其派生類繼承 5 靜態變數必須初始化後才能被呼叫 無論是成員函式還是靜態函式呼叫 靜...

C 類中的靜態成員

我們學習乙個知識點,首先想到的是為什麼要有這個東西的存在呢?對於c 類的靜態成員存在的理由如下 有的時候類需要它的一些成員與類本身無關,而不是與類的各個物件保持關聯。例如 乙個銀行賬戶型別類可能需要乙個資料成員來表示當前基準利率。在此例中,我們希望利率與類關聯,而非與類的每個物件關聯。從實現的效率的...

c 類中的靜態成員

靜態成員和非靜態成員的區別 class type class student 類的靜態成員包括 1 靜態資料成員 類的靜態資料成員儲存在全域性 靜態 儲存區,靜態資料成員定義時要分配儲存空間,而類定義本身不占用記憶體空間,只有在例項化為物件時才分配記憶體空間,因此靜態資料成員不能再類中進行定義,st...