Effective C++ item 2: Prefer consts, enums, and inlines to #defines.

To limit the scope of a constant to a class, we have to make it a member. And to ensure there’s at most one copy of the constant, we must make it a static member:

123456

classFoo{private:staticconstdouble RATIO = 1.5; ....};

Above is a declaration for RATIO, not a definition. C++ requires that we provide a definition for anything we use, but class-specific constants that are static and of integral type(e.g., integers, doubles, bools) are an exception. As long as we don’t take their address, we can declare them and use them without providing a definition. If you do want to take the address, then provide a definition like this:

1

constdouble Foo::RATIO;

We put this in a.cpp file not a.h file. Because the initial value of class constants is provided where the constant is declared, no initial value is permitted at the point of definition.