c++语言static作用,详解c++中的 static 关键字及作用

注:若没有特指是 静态成员时,默认都是普通成员;

1 类中的普通成员

类中的成员变量 和 成员函数 是分开存储的。其中,

1)每个对象都有独立的成员变量;成员变量可以存储在 栈空间、堆空间、全局数据区;

2)所有对象共享类的成员函数;成员函数 只能存储在 代码段;

2 类中的静态成员(static)

类中的静态成员

1、用 static关键字 修饰;

2、可以用 类名::成员名 访问 静态成员;

3、静态成员 属于 整个类;

4、静态成员 是所属类的成员,其它类不能访问;

5、静态成员的内存分配 是 唯一的;

1) 静态成员变量

特征:1、静态成员变量 属于 整个类所有;

2、静态成员变量的生命周期不依赖任何对象;(静态成员变量的生命周期在程序的运行期)

3、所有对象共享类的静态成员变量;

4、可以通过 类名 直接访问公有的静态成员变量;

5、可以通过 对象名 访问公有的静态成员变量;

6、静态成员变量 需要在类外单独分配空间;(类内声明、类外定义并初始化)

7、静态成员变量 在程序内部位于全局数据区,不计入类的内存计算。

原因/好处:使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则,保证了安全性还可以节省内存。

使用方法:

1、在类的内部,使用 static 修饰普通成员变量;

2、在类的外部(全局作用域),使用 Type ClassName::VarName = value 初始化,并申请存储空间;

注:静态成员变量不属于类的任何对象,所以并不是对象建立时被定义的,所以它不能由类的构造函数初始化,一般也不能在类内初始化;

/*

静态成员变量 只能在类的内部声明,在类的外部(全局区)定义和初始化;

*/

#include

using namespace std;

class Test{

public:

int GetA() const{return a;}

private:

static int a; // 静态成员变量

};

//int Test::a;如果这样定义不赋予初值,则初值为零

int Test::a = 1;

int main(int argc, char *argv[])

{

Test T;

cout << T.GetA() << endl;

return 0;

}

静态成员变量 被类的所有对象共享,包括派生类对象;

#include

using namespace std;

class Base{

public:

static int a; // 静态成员变量

};

// int Test::a;如果这样定义不赋予初值,则初值为零

int Base::a;

class Derived : public Base{

};

int main(int argc, char *argv[])

{

Base B;

Derived D;

B.a++;

cout << B.a << endl; // 1

D.a++;

cout << D.a << endl; // 2

return 0;

}

静态成员变量可以作为普通成员函数的默认形参,而普通成员变量则不可以;

class Test{

public:

static int a; //静态成员变量

int b;

void fun_1(int i = a); //正确

//void fun_2(int i = b); //报错

};

静态成员变量的类型 可以是所属类的类型,而普通成员变量则不可以。普通成员变量只能声明为 所属类类型的 指针或引用;

class Test{

public:

static Test a; //正确

Test b; //报错

Test *pTest; //正确

Test &m_Test; //正确

static Test *pStaticObject; //正确

};

静态成员变量在const函数中可以修改,而普通的成员变量是万万不能修改的;

/*

const修饰的是当前this指针所指向的对象是const,但是静态成员变量不属于任何类的对象,它被类的所有对象修改,所以this指针不修饰静态的成员变量,所以可以更改。

*/

class Test{

public:

static int a;

int b;

public:

Test():b(0){}

void test() const

{

a++;

//b++; // err // const指的是不能修改当前调用该函数对象的成员变量

}

};

int Test::a;

2)静态成员函数

特征:1、静态成员函数 属于 整个类所有;

2、所有对象共享类的静态成员函数;

2、可以通过 类名 直接访问公有的静态成员函数;

3、可以通过 对象名 访问公有的静态成员函数;

4、静态成员函数 只能 访问静态成员,不能访问 非静态成员;

5、静态成员函数没有this指针,也就是说静态成员函数不能使用修饰符(也就是函数后面的const关键字);

原因:处理静态成员变量;

使用方法:直接用 static 修饰 普通成员函数(类内声明时),不需要 static 修饰(类外定义时);

总结:

670a2371bc4d9afa5c96edf99db85dc6.png

案例分析:

/**

* 统计某班选修课考试的平均成绩

*/

#include

#include

using namespace std;

class Student

{

private:

string name; // 姓名

string gender; // 性别

float score; // 分数

string subject; // 课程

static int total_counts; // 总人数

static float chinese_scores; // 语文分数

static int chinese_counts; // 语文课人数

static float math_scores; // 数学分数

static int math_counts; // 数学课人数

public:

Student(string name, string gender, float score, string subject);

~Student();

static float aveScores(string subject);

void printStudentInfo();

void printAveScores();

};

int Student::total_counts = 0;

float Student::chinese_scores = 0;

int Student::chinese_counts = 0;

float Student::math_scores = 0;

int Student::math_counts = 0;

Student::Student(string name, string gender, float score, string subject)

{

this->name = name;

this->gender = gender;

this->score = score;

this->subject = subject;

if(subject == "chinese" || subject == "语文")

{

chinese_scores += score;

chinese_counts++;

}

else if(subject == "math" || subject == "数学")

{

math_scores += score;

math_counts++;

}

else

{

cout << "this is no the subect:" << subject << endl;

}

total_counts += (chinese_counts + math_counts);

}

Student::~Student()

{

total_counts--;

chinese_counts--;

math_counts--;

}

float Student::aveScores(string subject)

{

float ave_score = 0;

if(chinese_counts > 0 && subject == "chinese" || subject == "语文")

{

ave_score = (chinese_scores / chinese_counts);

//cout << subject << "\t" << chinese_counts << "\t" << chinese_scores << endl;

}

else if(math_counts > 0 && subject == "math" || subject == "数学")

{

ave_score = (math_scores / math_counts);

//cout << subject << "\t" <

}

return ave_score;

}

void Student::printStudentInfo()

{

cout << name << "\t" << gender << "\t" << score << "\t" << subject << endl;

}

void Student::printAveScores()

{

cout <

}

int main(int argc, char const *argv[])

{

const int SIZE = 5;

Student stu[SIZE] =

{

Student("10001", "male", 92, "语文"),

Student("10002", "male", 91, "数学"),

Student("10003", "male", 91, "数学"),

Student("10004", "male", 93, "语文"),

Student("10005", "male", 92, "语文"),

};

for(int i = 0; i < SIZE; i++)

{

stu[i].printStudentInfo();

}

stu[0].printAveScores();

stu[1].printAveScores();

cout << "语文" << " average score: " << Student::aveScores("语文") << endl;

cout << "数学" << " average score: " << Student::aveScores("数学") << endl;

return 0;

}

静态成员的案例分析

下面看下c语言中的static关键字的作用

在我们日常使用过程中,static通常有两个作用:

1、修饰变量

静态全局变量:全局变量前加static修饰,该变量就成为了静态全局变量。我们知道,全部变量在整个工程都可以被访问(一个文件中定义,其它文件使用的时候添加extern关键字声明 ),而在添加了static关键字之后,这个变量就只能在本文件内被访问了。因此,在这里,static的作用就是限定作用域。

静态局部变量:局不变量添加了static修饰之后,该变量就成为了静态局部变量。我们知道局部变量在离开了被定义的函数后,就会被销毁,而当使用static修饰之后,它的作用域就一直到整个程序结束。因此,在这里static的作用就是限定生命周期。

2、修饰函数

修饰函数则该函数成为静态函数,函数的作用域仅限于本文件,而不能被其它文件调用。

总结

以上所述是小编给大家介绍的c++中的 static 关键字及作用,希望对大家有所帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值