Posted on 2007-02-10 09:10
softgamer 閱讀(619)
評論(0) 編輯 收藏 引用 所屬分類:
學習日志
?? 靜態數據成員很像是全局變量,但static數據成員有類作用域,靜態成員可以是public,private或protected.
?? 靜態數據成員在文件范圍內必須進行一次初始化。類的public靜態成員可以通過類的任何對象訪問,也可以用二元作用域分辨符通過類名進行訪問。類的private和protected 靜態成員必須通過類的public成員函數或類的友元訪問。即使類沒有對象,但仍然有靜態成員。類沒有對象時,要想訪問public靜態類成員,只需在成員數據名前加上類名和二元作用域分辨符(::).要在類沒有對象時訪問private或protected 靜態類成員,則需要提供一個public靜態成員函數,并在調用函數時在函數名前面加上類名和二元作用域分辨符。
#File Test.h
#ifndef TEST_H
#define TEST_H
class Test
{
?? public:
???????? Test( const char * , const char * );
???????? ~Test();
???????? const char *GetFstString() const;
???????? const char *GetLstString() const;
???????? static int GetCount();
? private:
???????? char * strFst;
???????? char * strLst;
???????? static int count;?
};
#endif
---------------------------------------------------------------------------------
#File Test.cpp
#include <iostream>
using std::cout;
using std::endl;
#include <cstring>
#include <cassert>
#include "Test.h"
int Test::count = 0;
int Test::GetCount() { return count ; }
Test::Test( const char * sFst, const char * sLst )
{
?? strFst = new char[ strlen( sFst ) + 1 ];
?? assert( strFst != 0 );
?? strcpy( strFst, sFst );
?
?? strLst = new char[ strlen( sLst ) + 1 ];
?? assert( strLst != 0 );
?? strcpy( strLst, sLst );?
?? count++; // inc static count
?? cout << " Test cons for " << strFst
??????? << "? " << strLst << " called. " << endl;
}
?
Test::~Test()
{
?? cout << " ~Test() called for " << strFst
??????? << " " << strLst << endl;
?? delete [] strFst;
?? delete [] strLst;
?? count--;
}
const char * Test::GetFstString() const
{
??? return strFst;
}
const char * Test::GetLstString() const
{
??? return strLst;
}
--------------------------------------------------------------------------------
#File: main.cpp
#include <iostream>
using std::cout;
using std::endl;
#include "Test.h"
int main()
{
?? cout << "before cons is "
??????? << Test::GetCount() << endl; // use class name
?? Test *pTest1 = new Test( "FstTest1", "LstTest1" );
?? Test *pTest2 = new Test( "FstTest2", "LstTest2" );
?? cout << "after cons is "
??????? << pTest1->GetCount();? //using instan
?? cout << "\n\nTest1: "
??????? << pTest1->GetFstString()
??????? << " " << pTest1->GetLstString()
??????? << "\nTest2:"
??????? << pTest2->GetFstString()
??????? << " " << pTest2->GetLstString() << "\n\n" << endl;
?? delete pTest1;
?? pTest1 = 0;
?? delete pTest2;
?? pTest2 = 0;
?? cout << "num after deletion is "
??????? << Test::GetCount() << endl;
?? return 0;
??????
}
-------------------------------------------------------------------------------------------
Result:
before cons is 0
?Test cons for FstTest1? LstTest1 called.
?Test cons for FstTest2? LstTest2 called.
after cons is 2
Test1: FstTest1 LstTest1
Test2:FstTest2 LstTest2
?~Test() called for FstTest1 LstTest1
?~Test() called for FstTest2 LstTest2
num after deletion is 0
?? Test類在沒有對象時,仍然可以引用count成員,但是只能通過調用靜態成員函數GetCount()完成.
?? 沒有實例化的對象,一定是用類名調用Test::GetCount(),如果有實例化的對象,則可以用pTest1->GetCount()
調用。我們公司明確規定,所有靜態成員函數只能調用類名句柄,不能調用對象句柄,我覺得這樣很好。
?? 還有一點,如果成員函數不訪問非靜態數據成員和成員函數,可以將成員函數聲明為靜態,與非靜態成員函數不同的是,靜態成員函數沒有this指針,因為靜態類數據成員和成員函數是獨立于類對象而存才的。
?
?? 關于斷言(assert) , assert類宏在cassert頭文件中定義。用于測試條件值,注意assert不運行任何析構函數即可中止程序執行. assert不一定要在調試完成后刪除,只需在程序文件開頭(通常可以在編譯器選項中指定)插入語句
#define NDEBUG