3、静态变量和函数
1、全局变量
#include<iostream> using namespace std; int a = 1; void func(int &a) { a++; //自加运算 cout << a << endl; //打印a的值 } int main() { func(a); //调用 func(a); func(a); return 0; }
注意:在声明函数是,一定要加上 ‘&’,否则a的值不会变化。 或者用指针
分析:程序中变量a在主函数外定义为全局变量,打印出的值不断增加。全局变量具有全局的作用域,在一个源文件里定义全局变量,则所有源文件都可以访问这个变量,直到程序结束全局变量才被销毁。
2、局部变量
#include<iostream> using namespace std; int main() { int a; a = 4; cout << a << endl; { int b = 1; cout << b << endl; } cout << b << endl; return 0; }
分析:局部变量的作用域 是从定义开始,到 {} 结束
同样局部变量也不能跨函数访问。
#include<iostream> using namespace std; void func() { cout << a << endl; } int main() { int a; a = 4; func(); return 0; }
分析:a在主函数中声明和定义,但没有传递到func()中,就会出现语法错误。
3、静态变量
#include<iostream> using namespace std; static int a = 1; void func(int &a) { a++; //自加运算 cout << a << endl; //打印a的值 } int main() { func(a); //调用 func(a); func(a); return 0; }
声明静态变量在普通变量前加一个static关键字
静态变量与全局变量在存储方式上相同,区别是:非静态全局变量的作用域是整个源程序,当一个源程序是由多个源文件组成时,声明的非静态全局变量在每个源文件中都是有效的。而静态全局变量则限制了其作用域,只在其声明的源文件中有效,在同一源程序中不能使用它。
4、静态变量在c++中应用
举一个学生类的例子
#include<iostream> #include<string> using namespace std; class Student { public: Student(string name, int age, float store,int count); //声明构造函数 ~Student(); //声明析构函数 int GetCount(); private: string name; int age; float store; int count; //记录学生数量 }; Student::Student(string name, int age, float store,int count) { this->name = name; this->age = age; this->store = store; this->count = count; cout << "Student()................" << endl; } Student::~Student() { cout << "~Student()............." << endl; } int Student::GetCount() { return this->count; } int main() { Student stu1("jiang", 18, 99.3,1); cout << stu1.GetCount() << endl; Student stu2("wang", 19, 100,2); cout << stu2.GetCount() << endl; cout << endl << endl; return 0; }
分析:这段程序中count声明为类的私有成员。自能在类中访问。count在程序中是为了记录学生数量,而手动输入count的值没有意义。
所以,在调用构造函数是,count++,在调用析构函数是,count--;
还要注意的是需要count变量不会因为stu1的释放而消失,即count是属于student这个类的成员数据。
所以将count定义为静态数据成员。
以下程序一些修改
#include<iostream> #include<string> using namespace std; class Student { public: Student(string name, int age, float store); //声明构造函数 ~Student(); //声明析构函数 int GetCount(); private: string name; int age; float store; static int count; //定义静态数据成员变量 }; Student::Student(string name, int age, float store) { this->name = name; this->age = age; this->store = store; count++; //count++ cout << "Student()................" << endl; } Student::~Student() { count--; //count-- cout << "~Student()............." << endl; } int Student::GetCount() { return this->count; } int Student::count = 0; int main() { Student *stu1=new Student("jiang", 18, 99.3); //用指针来声明,方便之后挨个释放是观察count的值 cout << stu1->GetCount() << endl; Student *stu2=new Student("wang", 19, 100); cout << stu2->GetCount() << endl; cout << endl << endl; delete stu1; cout << stu2->GetCount() << endl; delete stu2; cout << Student::GetCount() << endl; return 0; }
程序实现:
分析:在程序中,每定义一个stu,count加1;而dlete只后,count减1;
类中静态数据成员的语法:
a、由于静态程序是属于类的,即不能根据对象来定义它;需要在主函数外定义
int Student::count = 0;
b、可以通过类名来访问它
cout << Student::count << endl;
但是由于是私有成员,因此不能直接访问,需要用到静态函数。
5、静态函数
以上面的程序为例
分析:与静态变量一样,它也属于类的函数,也可以通过类名来访问。静态成员函数不能够调用普通的成员函数和普通的成员变量,因为静态成员函数属于类,不知道普通的成员属性属于哪个对象,只能调用静态的类的资源。
6、静态变量与动态变量的区别
a、静态变量通常是在变量定义时就分定存储单元并一直保持不变,直至整个程序结束。静态变量,全局动态变量都是静态存储。
b、动态变量是在程序执行过程中,使用它时才分配存储单元,使用完毕立即释放。
c、静态变量只会初始化一次,重复初始化会出现语法错误。如果静态变量没有初始化,则系统自动定义为0,而动态变量为随机值。
d、全局数据区:static 数据, 全局变量, const常量。
堆区:由程序员自己new出来的动态数据, 需要手动释放。若忘记释放,会造成内存泄漏,则程序结束时会由操作系统回收。
栈区:函数内部变量,由IDE自动分配,结束时自动释放。