C++关键字之static

文章目录

  • C++ 关键字之static
    • 1.static关键字简介
    • 2.static修饰变量
      • 2.1.static修饰全局变量
      • 2.2.static修饰局部变量
      • 2.3.static修饰成员变量
    • 3.static修饰函数
      • 3.1.static修饰全局函数
      • 3.2.static修饰成员函数

C++ 关键字之static

1.static关键字简介

static关键字可以用来修饰函数或变量,修饰的变量大致可以分为全局变量局部变量,修饰的函数可以分为全局函数成员函数

2.static修饰变量

static修饰全局变量时,修饰的全局变量拥有文件作用域,内部链接性,整个程序生命周期内存在
static修饰局部变量时,修饰的局部变量拥有局部作用域,无链接性,整个程序生命周期内存在

需要注意的是:使用static修饰的变量只会进行一次初始化,未被初始化的静态变量将会被设置为“0”(“0”)对于不同类型的变量具有不同的意义,如 int类型的static变量会被初始化为0,而指针会被初始化为nullptr,我们可以大胆的做出猜想,对于类类型变量,未被初始化的static变量可能会调用其默认构造函数进行初始化

2.1.static修饰全局变量

static修饰全局变量时,该变量所在的文件内的所有结构都可以访问该变量。由于是内部链接性的,因此其他文件不能访问该变量,如果想要其他文件也能够访问该变量,要使用 extern关键字声明

#include 
#include 
using namespace std;

// 这里由于方便起见,就不定义类类型
struct Person {
     
	string name;
	Person() {
     
		name = "Jaron";
	}
};

// 定义全局静态变量, 未被初始化
static Person p;

int main() {
     
	cout << "name:	" << p.name << endl;
	return 0;
}

程序输出:
在这里插入图片描述
上图的程序输出果然验证了我们的猜想。

2.2.static修饰局部变量

static修饰的局部变量和全局变量的用法基本相同,这里使用一个例子演示。

#include 
using namespace std;

void Func() {
     
	// 定义局部静态变量,该变量只进行一次初始化
	static int times = 0;
	cout << "Func called " << ++times << " times." << endl;
}

int main() {
     
	for(int i = 0; i < 5; ++i) {
     
		// 静态变量在整个程序生命周期存在
		Func();
	}

	return 0;
}

程序截图:
C++关键字之static_第1张图片

2.3.static修饰成员变量

static修饰成员变量与修饰普通变量的用法基本相同,需要注意的是,static修饰成员变量时,所有的类成员共享同一份变量。且访问静态类成员时,可以使用作用域解析运算符,也可以使用类的对象访问,如果要修改类静态成员,必须使用静态成员函数

#include 
#include 
/***************************************************************
 * 我们定义一个银行账户类,银行账户的户主名是不能随意更改的,
 * 因此我们将其声明为const string类型的变量,因为所有的账户
 * 共享银行的利率信息,且不能使用账户类修改银行利率,因此
 * 我们将银行利率定义为const static double类型的,访问权限为public
 ***************************************************************/
class Account {
     
private:
	const std::string name;
public:
	// 静态成员常量的声明
	const static double InterestRate;
public:
	// 构造函数
	Account(std::string n);
	// name是不可修改的,不妨定义getName的返回值为const string &
	const std::string & getName();
};

// 静态成员常量的定义, 注意这里的写法
const double Account::InterestRate = 0.056;

Account::Account(std::string n) : name(n) {
     
}

const std::string & Account::getName() {
     
	return name;
}

using namespace std;
int main() {
     
	Account a1("Jaron");
	Account a2("Tom");

	cout << "Prints the addresses of static member "
		 << "variables in a1 a2 and class Account:"
		 << endl;

	cout << "In " << a1.getName() << ":	" << &a1.InterestRate << endl
		 << "In " << a2.getName() << ":		" << &a2.InterestRate << endl
		 // 使用作用域解析运算符访问
		 << "In Class Account: " << &Account::InterestRate << endl;

	return 0;
}

程序输出截图:
在这里插入图片描述

3.static修饰函数

static修饰的函数也可以分为全局函数和成员函数两种。

3.1.static修饰全局函数

static修饰的全局函数和普通全局函数没有区别,只不过是限定了全局函数只在此文件内可访问,其他文件不可访问该函数,若想在其他文件内访问该函数,应该使用extern声明该函数。

// file1
static void print() {
     
	cout << "Hello Jaron" << endl;
}

// file2
// 不可访问,编译期将报告这个错误
print();

// file3
extern void print();
// OK
print();

3.2.static修饰成员函数

使用static修饰成员函数时,被修饰的成员函数不能访问非静态成员变量,一般定义的static函数都是用来处理static静态成员的,也可以用其来完成一些与类无关的工作。像cmath中定义的函数,大多都是static的,如min、max、squrt等。下面是一个static修饰成员函数的实例。

// account.hpp
#include 
using std::string;

class CentralBank {
     
private:
	// 私有静态变量
	static double InterestRate;
public:
	// 必须使用静态函数进行修改
	static void setInterestRate(double rate);
	static double getInterestRate() {
     return InterestRate;}
};

// 初始化Bank类中的静态成员变量InterestRate
double CentralBank::InterestRate = 0.0;

class Account {
     
	// 声明Bank::setInterestRate(double rate);为该类的友元函数
	friend void CentralBank::setInterestRate(double rate);
private:
	const string name;
	// 将利率定义为私有的,并提供一个访问接口,但不可以使用Account类修改利率
	static double InterestRate;
public:
	Account(string n) : name(n) {
     }
	const string & getName() {
     return name;}
	static double getInterestRate() {
     return InterestRate;}
};

// 初始化Account类中的静态成员变量InterestRate
double Account::InterestRate = 0.0;

// 友元函数的定义要在其友元声明之后
// 注意,声明可以使用static关键字,定义时不行
void CentralBank::setInterestRate(double rate){
     
	// 调整利率 
	InterestRate = rate;
	// 调整Account的利率
	Account::InterestRate = rate;
}
// main.cpp
#include "account.hpp"
#include 
#include 
using namespace std;

int main() {
     
	Account a1("Jaron");
	Account a2("Tom");
	CentralBank::setInterestRate(0.0273);
	cout << "In " << a1.getName() << endl
		 << "interest rate: " << a1.getInterestRate() << endl
		 << "In " << a2.getName() << endl
		 << "interest rate: " << a2.getInterestRate() << endl
		 << "In class Account: " << endl
		 << "interest rate: " << Account::getInterestRate() << endl;

	cout << "In class CentralBank: " << endl
		 << "interest rate: " << CentralBank::getInterestRate() << endl;

	return 0;
}

程序输出截图:
C++关键字之static_第2张图片

你可能感兴趣的:(C++关键字)