c++static 限定作用域,只初始化一次
新特性:与函数,
不属于对象,属于类,没有this指针,没有对象属性,不能访问非静态(static)成员
static 访问static!
非static 访问 非static!
static
变量
1. 对象共享,不属于对象,属于类
2. 通过类名直接访问
函数
3. 没有this指针,无法访问非静态(变量,函数)
单例模式:
无法初始化
获取一个对象
static const int num=100;//代表此变量不属于对象的,是属于类的,所有对象会共用,除了const int 都在外部初始化
class Demo
{
public:
static int num;
int othet;
static bool isYear();//不属于对象,属于类,没有this指针,没有对象属性。
protected:
private:
};
Demo::num=100;
Demo demo1,demo2;//共用static num。
Demo::isYear(2016);
类的大小
只计算数据成员,不计算成员函数
静态不计入类的大小
虚函数和虚继承会占用类的大小,32占4,64占8