static data in c++ class

来源:互联网 发布:业余网络写手 编辑:程序博客网 时间:2024/05/29 15:06

e:\books\2013\c++\C++.Primer.Plus.6th.Edition.pdf

 

C++ has a second way of defining a constant within a class—using the keyword static:
class Bakery
{
private:
static const int Months = 12;
double costs[Months];
...
This creates a single constant called Months that is stored with other static variables
rather than in an object.Thus, there is only one Months constant shared by all Bakery
objects. Chapter 12 looks further into static class members. In C++98 you can use this
technique only for declaring static constants with integral and enumeration values.Thus,
C++98 doesn’t allow you to store a double constant this way. C++11 removes that
restriction.

chapter 12

A Review Example and Static Class Members