类中static的使用

来源:互联网 发布:龙霸网络电视手机版 编辑:程序博客网 时间:2024/05/19 02:26
class Year {static const int min = 1800;static const int max = 2200;public:class Invalid { };Year(int x) : y(x) {if ((x < min) || (x > max)){throw Invalid();}}int year() {return y;}private:inty;};

这就是我们在类中定义整型符号常量的方法。我们使用static限定一个类成员,可以保证它在整个程序中只有一份拷贝,而不是每个类对象都有一份。
原创粉丝点击