首页 > 其他 > 详细

类的静态成员

时间:2015-09-22 19:03:59      阅读:235      评论:0      收藏:0      [点我收藏+]
  1. 类里面static修饰的成员,成为静态类成员。

  2. 类的静态成员是该类型的所有对象对象所共享。

技术分享

静态成员的定义及使用

class Date

{

public :

     Date ()

    {

          cout<<"Date ()" <<endl;

         ++ sCount;

    }


     void Display ()

    {

          cout<<"year:" <<_year<< endl;

          cout<<"month:" <<_month<< endl;

          cout<<"day:" <<_day<< endl;

    }


     // 静态成员函数

     static void PrintCount()

    {

          cout<<"Date count:" <<sCount<< endl;

    }

private :

     int _year ;     // 

     int _month ;    // 

     int _day ;      // 


private :

     static int sCount; // 静态成员变量,统计创建时间个数

};


// 定义并初始化静态成员变量

int Date::sCount = 0;


void Test ()

{

     Date d1 ,d2;


     // 访问静态成员

     Date::PrintCount ();

}


类的静态成员

原文:http://10622551.blog.51cto.com/10612551/1697121

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!