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 初始化,并申请存储空间;

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

1 /*
2 静态成员变量 只能在类的内部声明,在类的外部(全局区)定义和初始化;
3 */
4
5 #include
6
7 using namespace std;
8
9 class Test{
10 public:
11 int GetA() const{return a;}
12 private:
13 static int a; // 静态成员变量
14 };
15 //int Test::a;如果这样定义不赋予初值,则初值为零
16 int Test::a = 1;
17
18 int main(int argc, char *argv[])
19 {
20 Test T;
21
22 cout << T.GetA() << endl;
23
24 return 0;
25 }
  静态成员变量 被类的所有对象共享,包括派生类对象;

1 #include
2
3 using namespace std;
4
5 class Base{
6 public:
7 static int a; // 静态成员变量
8 };
9
10 // int Test::a;如果这样定义不赋予初值,则初值为零
11 int Base::a;
12
13 class Derived : public Base{
14
15 };
16
17
18 int main(int argc, char *argv[])
19 {
20 Base B;
21 Derived D;
22
23 B.a++;
24 cout << B.a << endl; // 1
25 D.a++;
26 cout << D.a << endl; // 2
27
28 return 0;
29 }
  静态成员变量可以作为普通成员函数的默认形参,而普通成员变量则不可以;

1 class Test{
2 public:
3 static int a; //静态成员变量
4 int b;
5 void fun_1(int i = a); //正确
6 //void fun_2(int i = b); //报错
7 };
  静态成员变量的类型 可以是所属类的类型,而普通成员变量则不可以。普通成员变量只能声明为 所属类类型的 指针或引用;

1 class Test{
2 public:
3 static Test a; //正确
4 Test b; //报错
5 Test *pTest; //正确
6 Test &m_Test; //正确
7 static Test *pStaticObject; //正确
8 };
  静态成员变量在const函数中可以修改,而普通的成员变量是万万不能修改的;

1 /*
2   const修饰的是当前this指针所指向的对象是const,但是静态成员变量不属于任何类的对象,它被类的所有对象修改,所以this指针不修饰静态的成员变量,所以可以更改。
3 */
4 class Test{
5 public:
6 static int a;
7 int b;
8 public:
9 Test():b(0){}
10 void test() const
11 {
12 a++;
13 //b++; // err // const指的是不能修改当前调用该函数对象的成员变量
14 }
15 };
16
17 int Test::a;
 2)静态成员函数

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

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

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

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

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

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

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

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

总结:c++中的 static 关键字_第1张图片

案例分析:

1 /**
2 * 统计某班选修课考试的平均成绩
3 */
4
5 #include
6 #include
7
8 using namespace std;
9
10 class Student
11 {
12 private:
13 string name; // 姓名
14 string gender; // 性别
15 float score; // 分数
16 string subject; // 课程
17 static int total_counts; // 总人数
18 static float chinese_scores; // 语文分数
19 static int chinese_counts; // 语文课人数
20 static float math_scores; // 数学分数
21 static int math_counts; // 数学课人数
22 public:
23 Student(string name, string gender, float score, string subject);
24 ~Student();
25 static float aveScores(string subject);
26 void printStudentInfo();
27 void printAveScores();
28 };
29 int Student::total_counts = 0;
30 float Student::chinese_scores = 0;
31 int Student::chinese_counts = 0;
32 float Student::math_scores = 0;
33 int Student::math_counts = 0;
34
35 Student::Student(string name, string gender, float score, string subject)
36 {
37 this->name = name;
38 this->gender = gender;
39 this->score = score;
40 this->subject = subject;
41
42 if(subject == “chinese” || subject == “语文”)
43 {
44 chinese_scores += score;
45 chinese_counts++;
46 }
47 else if(subject == “math” || subject == “数学”)
48 {
49 math_scores += score;
50 math_counts++;
51 }
52 else
53 {
54 cout << “this is no the subect:” << subject << endl;
55 }
56 total_counts += (chinese_counts + math_counts);
57 }
58
59 Student::~Student()
60 {
61 total_counts–;
62 chinese_counts–;
63 math_counts–;
64 }
65
66 float Student::aveScores(string subject)
67 {
68 float ave_score = 0;
69
70 if(chinese_counts > 0 && subject == “chinese” || subject == “语文”)
71 {
72 ave_score = (chinese_scores / chinese_counts);
73 //cout << subject << “\t” << chinese_counts << “\t” << chinese_scores << endl;
74 }
75 else if(math_counts > 0 && subject == “math” || subject == “数学”)
76 {
77 ave_score = (math_scores / math_counts);
78 //cout << subject << “\t” <
79 }
80
81 return ave_score;
82 }
83
84 void Student::printStudentInfo()
85 {
86 cout << name << “\t” << gender << “\t” << score << “\t” << subject << endl;
87 }
88
89 void Student::printAveScores()
90 {
91 cout <" average score: " << aveScores(subject) << endl;
92 }
93
94 int main(int argc, char const *argv[])
95 {
96 const int SIZE = 5;
97 Student stu[SIZE] =
98 {
99 Student(“10001”, “male”, 92, “语文”),
100 Student(“10002”, “male”, 91, “数学”),
101 Student(“10003”, “male”, 91, “数学”),
102 Student(“10004”, “male”, 93, “语文”),
103 Student(“10005”, “male”, 92, “语文”),
104 };
105
106 for(int i = 0; i < SIZE; i++)
107 {
108 stu[i].printStudentInfo();
109 }
110
111 stu[0].printAveScores();
112 stu[1].printAveScores();
113
114 cout << “语文” << " average score: " << Student::aveScores(“语文”) << endl;
115 cout << “数学” << " average score: " << Student::aveScores(“数学”) << endl;
116
117 return 0;
118 }
深圳网站建设www.sz886.com

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值