static成员变量,staitic成员函数以及 实现只能实例化3次的类

来源:互联网 发布:淘宝上面卖中药的店 编辑:程序博客网 时间:2024/05/26 09:54

一、static 成员变量

对于特定类型的全体对象而言,有时候可能需要访问一个全局的变量。比如说统计某种类型对象已创建的数量。
如果我们用全局变量会破坏数据的封装,一般的用户代码都可以修改这个全局变量,这时可以用类的静态成员来解决这个问题。
非static数据成员存在于类类型的每个对象中,static数据成员独立该类的任意对象存在,它是与类关联的对象,不与类对象关联。

(1)、static成员的定义

static成员需要在类定义体外进行初始化与定义

(2)、特殊的整型static const成员

整型static const成员可以在类定义体中初始化,该成员可以不在类体外进行定义

(3)、static成员优点:

static成员的名字是在类的作用域中,因此可以避免与其它类成员或全局对象名字冲突。

可以实施封装,static成员可以是私有的,而全局对象不可以

阅读程序容易看出static成员与某个类相关联,这种可见性可以清晰地反映程序员的意图。


 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef _COUNTED_OBJECT_H_
#define _COUNTED_OBJECT_H_

class CountedObject
{
public:
    CountedObject();
    ~CountedObject();
public:
    static int GetCount();
private:
    static int count_;      // 静态成员的引用性声明
};


#endif // _COUNTED_OBJECT_H_


 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "CountedObject.h"

int CountedObject::count_ = 0;      // 静态成员的定义性声明

CountedObject::CountedObject()
{
    ++count_;
}

CountedObject::~CountedObject()
{
    --count_;
}

int CountedObject::GetCount()
{
    return count_;
}

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "CountedObject.h"
#include <iostream>
using namespace std;

int main(void)
{
    //cout<<CountedObject::count_<<endl;
    cout << CountedObject::GetCount() << endl;
    CountedObject co1;
    //cout<<CountedObject::count_<<endl;
    cout << CountedObject::GetCount() << endl;
    CountedObject *co2 = new CountedObject;
    //cout<<CountedObject::count_<<endl;
    cout << CountedObject::GetCount() << endl;
    delete co2;
    //cout<<CountedObject::count_<<endl;
    cout << CountedObject::GetCount() << endl;

}

上述程序定义一个静态成员变量和静态成员函数,可以通过类名:: 访问static 成员变量,也可以通过非/静态成员函数访问。


二、static 成员函数

static成员函数没有隐含的this指针
非静态成员函数可以访问静态成员
静态成员函数不可以访问非静态成员


 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;

class Test
{
public:
    Test(int y) : y_(y)
    {

    }
    ~Test()
    {

    }

    void TestFun()
    {
        cout << "x=" << x_ << endl; //OK,非静态成员函数可以访问静态成员
        TestStaticFun();
    }
    static void TestStaticFun()
    {
        cout << "TestStaticFun ..." << endl;
        //TestFun();        Error,静态成员函数不能调用非静态成员函数
        //cout<<"y="<<y_<<endl;     Error,静态成员函数不能访问非静态成员
    }
    static int x_;      // 静态成员的引用性说明
    int y_;
};

int Test::x_ = 100;     // 静态成员的定义性说明

int main(void)
{
    cout << sizeof(Test) << endl;

    return 0;
}

三、使用C++设计一个类,该类最多能被实例化3次且不能被继承

[cpp] view plaincopyprint?
  1. /*用C++实现一个LimitedClass类,该类最多只能被实例化三次,且不能被继承*/  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. class LimitedClass  
  6. {  
  7. public:  
  8.     static LimitedClass* getInstance();  
  9.     static void setCnt(int);//设置能实例化的次数  
  10.     ~LimitedClass();  
  11.     //其它方法;  
  12. private:  
  13.     LimitedClass();//私有构造函数,无法被继承  
  14.     static int cnt;//实例化次数  
  15. };  
  16.   
  17. int LimitedClass::cnt=0;//实例化次数类外定义  
  18. LimitedClass* LimitedClass::getInstance()  
  19. {  
  20.     if(cnt>0)  
  21.     {  
  22.         --cnt;  
  23.         return new LimitedClass();  
  24.     }  
  25.     else  
  26.         return NULL;  
  27. }  
  28.   
  29. LimitedClass::LimitedClass()  
  30. {  
  31.     cout<<"LimitedClass Constructor!!!"<<endl;  
  32. }  
  33.   
  34. LimitedClass::~LimitedClass()  
  35. {  
  36.     cout<<"LimitedClass Destructor!!!"<<endl;  
  37. }  
  38.   
  39. void LimitedClass::setCnt(int n)  
  40. {  
  41.     cnt=n;  
  42. }  
  43. //测试程序  
  44. int main()  
  45. {  
  46.     LimitedClass::setCnt(3);  
  47.     LimitedClass *l1=LimitedClass::getInstance();  
  48.     LimitedClass *l2=LimitedClass::getInstance();  
  49.     LimitedClass *l3=LimitedClass::getInstance();  
  50.     if (l1!=NULL && l2!=NULL && l3!=NULL)  
  51.     {  
  52.         cout<<"实例化成功3个对象"<<endl;  
  53.     }  
  54.     LimitedClass *l4=LimitedClass::getInstance();  
  55.     if (NULL==l4)  
  56.         cout<<"第四个实例无法实例化"<<endl;  
  57.   
  58.     delete l1;  
  59.     delete l2;  
  60.     delete l3;  
  61.     return 0;  
  62. }