通关旅

softgamer 的痕迹
posts - 12, comments - 0, trackbacks - 0, articles - 1

C++体会 -- 静态数据成员

Posted on 2007-02-10 09:12 softgamer 阅读(296) 评论(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



只有注册用户登录后才能发表评论。


网站导航: