类static成员
类static成员引进:有时特定类的全体对象需要访问一个全局对象。
★为什么用static成员而不是全局对象?
---- static对象名字在类作用域中,可以有效避免命名冲突,并且清晰显示程序意图。
---- 可以实施封装(static成员可以定义为private,而全局对象不可以)
★如何调用? ---- class A a; A::static_mem; a.static_mem;
★声明和定义
---- static成员函数在类定义体内部外部定义均可。在类定义体外定义时不加(不可以加)static修饰。
---- static数据成员必须在类定义体外部定义(正好一次)(在类定义体外声明并定义)
class A{public : static int n;} int A::n = 10;
---- const static数据成员可以(也可以不)在类定义内初始化,但必须在类定义体外部重新声明(不可以加static修饰符,不可以赋初值)
★static数据成员其他特性:
----一般地,在类的内部不能有该类类型的变量(或函数形参),最多只能有该类类型的指针或引用做变量类型(或函数形参). static数据成员则不受这个限制。
---- 类的static数据成员可以作为函数默认实参
---- static成员的继承:如果基类有static成员,则整个继承层次中只有一个这样的成员。每个static成员总是只有一个实例。
1
#include <iostream>
2
#include <cstdlib>
3![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
4
using namespace std;
5![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
6
class A
7
{
8
//重载输出操作符
9
friend ostream& operator<<(ostream& out, const A&a)
10
{
11
out << a.m;
12
return out;
13
}
14
15
private:
16
int m ;
17
18
public:
19
A(int i) { m = i;}
20
21
static int n;
22
23
static A a; //类可以有该类类型的static成员
24
//A a1; //error. 类不可以有普通的该类类型的成员
25
26
static void func(A a) {cout << a << endl;} //类类型可以作为该类static函数的形参
27
28
//static 数据成员可以作为类成员函数的默认实参
29
static void func2(int i = n) { cout << i*i <<endl;}
30
};
31![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
32
int A::n = 10;
33
A A::a(1);
34![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
35
int main()
36
{
37
cout << A::n << endl; //10
38
cout << A::a <<endl; //1
39
40
A a2(3);
41
A::func(a2); //3
42
A::func2(); //100
43
44
system("pause");
45
return 0;
46
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
2
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
3
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
4
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
5
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
6
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
7
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
16
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
18
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
24
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
26
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
29
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
31
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
32
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
33
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
34
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
35
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
36
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
37
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
38
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
39
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
40
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
41
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
42
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
43
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
44
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
45
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
46
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
转载请注明出处: http://www.cnblogs.com/liyuxia713/