在C++中,对于类可以具有静态成员和静态函数,他们都是用static关键字修饰。
首先需要知道的是,为什么要使用静态成员。
对于非静态成员,一定是每个类对象持有该非静态数据成员的一份拷贝,如果某个非静态数据成员的值是固定的,比如利率、价格(对于同一种商品)等等,事实上根本无需让每个类对象都拷贝一份该类的该数据成员,如果该类的对象数量很大时,在一定程度上冗余存储了该数据成员,造成存储空间的浪费。
对于静态成员来说,通常如果一个类所具有的数据成员对于类的每个实例来说都是一样的,也就是说该数据成员的变化不是很大,即使变化也需要对该类的全部实例都修改,这样,就要考虑使用静态成员,静态成员是属于类的,在多个该类实例共存的情况下,该静态数据成员只是具有一份拷贝,达到共享且节省内存的目的。
在C++中,对于静态成员,可以声明为private的,即私有的,在类的外部是没有权限访问的,只能在类的内部使用。当静态成员被声明为public的,就相当于全局对象,只是在类的外部被访问时需要加上类域操作符。
如果类的静态成员不是非常量有序类型(例如const int/const double/const float/const short)的,就不能在类的定义中进行初始化,例如在自己定义System类的static.h头文件中:
class System {
public:
System();
System(int year);
void start(int method);
void work(int howlong);
static string& get_xcode();
int get_made_year();
static int get_durable();
~System();
private:
static string xcode;
int made_year;
static const int durable = 40000;
public:
static const string name = "X00125 AUTOSYS"; // 这样在类定义中初始化是错误的
};
对于类的静态数据成员,无论是private还是public的,都可以在类实现中进行初始化,例如在static.cpp中初始化上面定义的两个静态数据成员name和xcode,如下所示:
const string System::name("X00125 AUTOSYS");
string System::xcode("J862009-X007");
当然,对于可比较类型的静态数据成员也可以在类实现文件中进行初始化,也可以在类的定义头文件中进行初始化。
下面给出一个正确的System类的定义头文件static.h,代码如下:
#include <string>
using namespace std;
class System {
public:
System();
System(int year);
void start(int method);
void work(int howlong);
static string& get_xcode();
int get_made_year();
static int get_durable();
~System();
private:
static string xcode;
int made_year;
static const int durable = 40000;
public:
static const string name;
};
可见,数据成员包含静态的数据成员xcode、durable和name,非静态数据成员made_year;然后就是成员函数声明部分,成员函数包括两个静态成员函数get_xcode()和get_durable(),其余的都是非静态的。
在类定义中,定义了private的静态成员xcode和durable,它们只能在类的内部被使用,如果想要访问它们的数据必须通过提供的public的静态成员函数来实现,否则它们对外部来说是不可见的;而对于public修饰的静态数据成员,如上面的name,就可以被类的外部公共访问,只需要加上类域操作符System::即可实现访问public的静态数据成员。
对上面类System定义的static.h头文件,类实现部分放在static.cpp文件中实现,如下所示:
#include <iostream>
#include "static.h"
using namespace std;
const string System::name("X00125 AUTOSYS");
string System::xcode("J862009-X007");
System::System() {}
System::System(int year) : made_year(year) {}
void System::start(int method) {
switch(method) {
case 1:
cout<<"Hot Start!!!"<<endl;
break;
case 2:
cout<<"Freeze Start!!!"<<endl;
break;
default:
cout<<"Stop!!!"<<endl;
}
}
void System::work(int howlong) {
cout<<"It has been worked for "<<howlong<<" hours."<<endl;
}
string& System::get_xcode() {
return xcode;
}
int System::get_made_year() {
return made_year;
}
int System::get_durable() {
return durable;
}
System::~System() {}
这里要说明几点:
1、类的静态成员函数不能使用const和volatile来修饰;
2、类的静态成员函数没有隐式的this指针;
3、类的静态成员函数在类体外(实现该类定义的程序部分)不能使用static关键字来修饰。
再写一个,用来测试访问类的静态数据成员和成员函数的主函数,放在main.cpp文件中:
#include <iostream>
#include "static.h"
int main() {
cout<<System::name<<endl;
cout<<System::get_xcode()<<endl;
cout<<System::get_durable()<<endl;
int year = 2009;
System *system = new System(year);
system->start(1);
system->work(1788);
cout<<system->get_made_year()<<endl;
delete system;
return 0;
}
编译运行,如下所示:
[root@localhost class]# g++ -Wall -o main static.cpp main.cpp
[root@localhost class]# ./main
X00125 AUTOSYS
J862009-X007
40000
Hot Start!!!
It has been worked for 1788 hours.
2009
[root@localhost class]#
上面是关于含有静态成员或静态成员函数的类的最基本的