一、static 成员变量
对于特定类型的全体对象而言,有时候可能需要访问一个全局的变量。比如说统计某种类型对象已创建的数量。
如果我们用全局变量会破坏数据的封装,一般的用户代码都可以修改这个全局变量,这时可以用类的静态成员来解决这个问题。
非static数据成员存在于类类型的每个对象中,static数据成员独立该类的任意对象存在,它是与类关联的对象,不与类对象关联。
(1)、static成员的定义
static成员需要在类定义体外进行初始化与定义
(2)、特殊的整型static const成员
整型static const成员可以在类定义体中初始化,该成员可以不在类体外进行定义
(3)、static成员优点:
static成员的名字是在类的作用域中,因此可以避免与其它类成员或全局对象名字冲突。
可以实施封装,static成员可以是私有的,而全局对象不可以
阅读程序容易看出static成员与某个类相关联,这种可见性可以清晰地反映程序员的意图。
C++ Code
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_ |
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
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指针
非静态成员函数可以访问静态成员
静态成员函数不可以访问非静态成员
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次且不能被继承
- /*用C++实现一个LimitedClass类,该类最多只能被实例化三次,且不能被继承*/
- #include <iostream>
- using namespace std;
- class LimitedClass
- {
- public:
- static LimitedClass* getInstance();
- static void setCnt(int);//设置能实例化的次数
- ~LimitedClass();
- //其它方法;
- private:
- LimitedClass();//私有构造函数,无法被继承
- static int cnt;//实例化次数
- };
- int LimitedClass::cnt=0;//实例化次数类外定义
- LimitedClass* LimitedClass::getInstance()
- {
- if(cnt>0)
- {
- --cnt;
- return new LimitedClass();
- }
- else
- return NULL;
- }
- LimitedClass::LimitedClass()
- {
- cout<<"LimitedClass Constructor!!!"<<endl;
- }
- LimitedClass::~LimitedClass()
- {
- cout<<"LimitedClass Destructor!!!"<<endl;
- }
- void LimitedClass::setCnt(int n)
- {
- cnt=n;
- }
- //测试程序
- int main()
- {
- LimitedClass::setCnt(3);
- LimitedClass *l1=LimitedClass::getInstance();
- LimitedClass *l2=LimitedClass::getInstance();
- LimitedClass *l3=LimitedClass::getInstance();
- if (l1!=NULL && l2!=NULL && l3!=NULL)
- {
- cout<<"实例化成功3个对象"<<endl;
- }
- LimitedClass *l4=LimitedClass::getInstance();
- if (NULL==l4)
- cout<<"第四个实例无法实例化"<<endl;
- delete l1;
- delete l2;
- delete l3;
- return 0;
- }