类的静态成员:
1.静态数据成员:
如果某个属性为整个类所有,不属于任何一个具体对象,则采用static关键字声明为静态成员。静态成员在每个类中只有一份拷贝,由类的各个对象共同维护,保证了该数据成员的共享。静态数据成员具有静态生存期,由于静态数据成员不属于任何一个对象,通过类名来访问它。
一个恰当的例子:如果把类比作一个工厂,那么对象是工厂生产的产品,静态数据成员是属于工厂的,而不是属于产品的
#include <iostream>
using namespace std;
#include <cstdio>
#include <cstdlib>
class Point{
public:
Point(int xx = 0,int yy = 0){x = xx; y = yy;cntP++;}
Point(Point &p);
~Point(){cntP--;}
int getx(){return x;}
int gety(){return y;}
void getc(){cout << "object id = " << cntP << endl;}
private:
int x,y;
static int cntP; // 声明
};
Point :: Point(Point &p){
x = p.x;
y = p.y;
cntP++;
}
int Point::cntP = 0; // 定义
int main(){
Point A(4,5);
cout << "Point A," << A.getx() <<"," << A.gety();
A.getc();
Point B(A);
cout << "Point B," << B.getx() <<"," << B.gety();
B.getc();
system("pause");
}
如果 通过 Point :: getc(); 会发现出现编译错误,因为访问类的普通成员函数必须通过对象访问,如果把getc 设置为statci void getc() 类就可以访问了,运行结果如下:
静态 成员函数可以直接访问该类的静态数据和成员函数,而访问非静态数据必须通过参数传递方式得到对象名,然后通过对象名访问。
class A{
public:
static void f(A a);
private:
int x;
};
static void f(A a){
cout << x << endl; // 对x引用是错误的,不能直接访问
cout << a.x << endl; // 正确
}
2.const
共享数据的保护:对于需要共享,又需要防止改变的数据应该声明为常量.
1)常引用(引用的对象不能被更新)
#include <iostream>
using namespace std;
#include <cstdio>
#include <cstdlib>
void display(const double &r){ // d与r共用存储单元
cout << r << endl;
}
int main(){
double d(9.6);
display(d);
system("pause");
}
const 理解为只读更为合适,如果代码改成
void display(const double &r){ // d与r共用存储单元
(1) r = 7.2
(2) double s = 0.1;
(2) r = s;
cout << r << endl;
}
上面的(1)和(2)方案由于都对常引用r做了写处理,所以都会报下面的错误
2)常对象
常对象必须进行初始化,而且不能被更新
class A{
public:
A(int i,int j){x = i; y = j;}
private:
int x, y;
};
const A a(3,4); // a 是常对象,不能被更新
常对象不能调用普通的成员函数,只能调用常函数,一般对象可以调用常函数,当然也可以调用普通函数
3)常成员函数
第一,const在定义函数的时候要加入该关键字
第二,const函数不能修改对象的数据成员,也不能调用没有用const修饰的成员函数
第三,const 对象只能调用const函数
第四,const可以用于对重载函数的区分
#include <iostream>
using namespace std;
#include <cstdio>
#include <cstdlib>
class R{
public:
R(int r1,int r2){
R1 = r1;
R2 = r2;
}
void print();
void print()const;
private:
int R1,R2;
};
void R::print(){
cout << R1 << ":" << R2 << endl;
}
void R:: print()const{
cout << R1 << ":" << R2 << endl;
}
int main(){
R a(5,4);
a.print();
const R b(20,52);
b.print();
system("pause");
}
void R::print(){
R1 = 2;
R2 = 4;
cout << R1 << ":" << R2 << endl;
}
如果print() 这样修改,运行结果如下,运行结果说明不带const的修饰符可以更改对象的数据成员
如果更改void print() const 函数如下:
void R:: print()const{
R1 = 2;
R2 = 4;
cout << R1 << ":" << R2 << endl;
}
那么报错误如下,出现了read-only的关键字,所以不能运行:
4)常数据成员
类的成员数据数据可以是常量和常引用,使用const说明的数据都是常数据成员
如果在一个类中声明了常数据成员,那么任何其他函数都不能对其进行赋值
构造函数对该数据成员进行初始化,就只能通过初始化列表.(不能通过构造函数函数体)
#include <iostream>
using namespace std;
#include <cstdio>
#include <cstdlib>
class A{
public:
A(int i);
void print();
const int &r;
private:
const int a;
static const int b;
};
const int A::b = 10;
A::A(int i):a(i),r(a){}
void A::print(){
cout << a << ":" << b << ":" << r << endl;
}
int main(){
A a1(100),a2(0);
a1.print();
a2.print();
system("pause");
}
A::A(int i){
a = i;
r = a;
}
这个是赋值,不是初始化,所以报错如下:使用引用时注意以下问题:
声明一个引用时,必须同时对它进行初始化,使它指向一个已经存在的对象
一旦一个引用被初始化后,就必须确定是哪个变量的别名,而且只能始终作为这一个变量的别名