VC++ 申明静态变量的注意事项

开发工具VC++ 6.0,举例类CEmployee

按照习惯,我们在头文件中如果已经声明了变量,我们在源文件中就可以直接使用了。

   1: protected:
   2:     static string m_EmployeeName;

我们在构造函数初始化它,

   1: CEmployee::CEmployee()
   2: {
   3:  
   4:     m_EmployeeName ="Rock";
   5: }

编译没有问题,但连接的时候有问题:

Linking...
Employee.obj : error LNK2001: unresolved external symbol "protected: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > CEmployee::m_EmployeeName" (?m_EmployeeName@CEmployee@@1V?$basic_string@DU?$char_trai
ts@D@std@@V?$allocator@D@2@@std@@A)
Debug/TestStaticVariable.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

正确的申明方式应该做到两点:

1. 在头文件中申明

2. 在源文件中再次声明。

头文件中:

   1: private:
   2:     static string m_EmployeeName;
   3:     static int m_count;

源文件:

   1: string CEmployee::m_EmployeeName;
   2: int CEmployee::m_count;
   3:  
   4:  
   5:  
   6: CEmployee::CEmployee()
   7: {
   8:  
   9:     m_EmployeeName ="Rock";
  10:     m_count++;
  11: }

这样的申明方式很容易让人疑惑。

疑惑一:源文件再次申明静态变量不需要再加static前缀。

疑惑二:理论上,静态变量是不可继承的,因此访问方式只能是类的公开或私有。但是,我们把它定义为保护类型编译运行也没有错。

   1:  
   2: protected:
   3:     static string m_EmployeeName;
   4:     static int m_count;
   5:     

我经常使用java或C#, 对于这种违背OO的语法自然有所疑惑. 不过:C++确实是有其与OO不同的语法之处.

为了测试静态变量是否确实像我们想象那样,我定义几个函数,并做了测试.

注意:静态函数的申明也如同静态变量一样.

   1: // Employee.h: interface for the CEmployee class.
   2: //
   3: //////////////////////////////////////////////////////////////////////
   4:  
   5: #if !defined(AFX_EMPLOYEE_H__CD070057_6BC8_4639_BA5B_8EBD94A191BD__INCLUDED_)
   6: #define AFX_EMPLOYEE_H__CD070057_6BC8_4639_BA5B_8EBD94A191BD__INCLUDED_
   7:  
   8: #if _MSC_VER > 1000
   9: #pragma once
  10: #endif // _MSC_VER > 1000
  11: #include <iostream>
  12: using namespace std;
  13:  
  14: class CEmployee  
  15: {
  16:  
  17: public:
  18:     CEmployee();
  19:     virtual ~CEmployee();
  20:     static intilizeCount(int in_count);
  21:     int& getCount();
  22:  
  23:     
  24:  
  25: protected:
  26:     static string m_EmployeeName;
  27:     static int m_count;
  28:     
  29:  
  30: };
  31:  
  32: #endif // !defined(AFX_EMPLOYEE_H__CD070057_6BC8_4639_BA5B_8EBD94A191BD__INCLUDED_)
   1: // Employee.cpp: implementation of the CEmployee class.
   2: //
   3: //////////////////////////////////////////////////////////////////////
   4:  
   5: #include "stdafx.h"
   6: #include "Employee.h"
   7:  
   8: //////////////////////////////////////////////////////////////////////
   9: // Construction/Destruction
  10: //////////////////////////////////////////////////////////////////////
  11:  
  12: string CEmployee::m_EmployeeName;
  13: int CEmployee::m_count;
  14:  
  15:  
  16:  
  17: CEmployee::CEmployee()
  18: {
  19:  
  20:     m_EmployeeName ="Rock";
  21:     m_count++;
  22: }
  23:  
  24: CEmployee::~CEmployee()
  25: {
  26:  
  27: }
  28:  
  29: CEmployee::intilizeCount(int in_count)
  30: {
  31:     m_count = in_count;
  32: }
  33: int& CEmployee::getCount()
  34: {
  35:     return m_count;
  36: }

main文件:

   1: // TestStaticVariable.cpp : Defines the entry point for the console application.
   2: //
   3:  
   4: #include "stdafx.h"
   5: #include "Employee.h"
   6: #include <iostream>
   7: using namespace std;
   8:  
   9: int main(int argc, char* argv[])
  10: {
  11:     //静态调用,用类的静态函数来初始化静态变量
  12:     CEmployee::intilizeCount(0);
  13:  
  14:     
  15:     CEmployee empl;
  16:     printf("Now current instance count: %d \n", empl.getCount());
  17:  
  18:     CEmployee empl2;
  19:     printf("Now current instance count: %d \n", empl.getCount());
  20:  
  21:  
  22:     CEmployee empl3;
  23:     printf("Now current instance count: %d \n", empl.getCount());
  24:  
  25:  
  26:  
  27:     cin.get();
  28:     
  29:     return 0;
  30: }
  31:  

image

可以看见,运行正常.

posted on 2009-09-27 14:01  一望无际的南  阅读(2337)  评论(0编辑  收藏  举报

导航