有时,使符号常量的做用域为类颇有用:对象
class Bakery { private: const int Months = 12; // declare a constant? FALSE double costs[Months]; ...
但这是行不通的,由于——声明类只是描述了对象的形式,并无建立对象。所以,在建立对象前,将没有用于存储值的空间。blog
——在类中声明一个枚举作用域
class Bakery { private: enum {Months = 12}; double costs[Months]; ...
——使用static编译器
class Bakery { private: static const int Months = 12; double costs[Months]; ...