Why should I declare a private static const
variable in header (and initialize it in cpp) instead of just defining + declaring it in the cpp?
i.e.
case1.h
class MyClass
{
...
private:
static const MyType some_constant;
}
case1.cpp
const MyType MyClass::some_constant = ...;
case2.h
//No mention of some_constant at all
case2.cpp
const MyType some_constant = ...;
Assuming common c++ conventions are followed (1 header & cpp is only associated with 1 class, never #include
.cpp file), in both cases, the variable is private to the class, both are initialized before the constructor is called, both provide the function of being a "static class local constant".
Is there any difference between the above two approaches? (And which one is preferable)?