Possible duplicate:Static member class and regular c-shaped interface
I look at the code of some bodies, and there are several dozen constants defined in the class as follows:
// header file class Defines { public: static const int Val1; static const int ValN; static const char* String1; static const char* StringN; ... } // .CPP const char* Defines::String1 = "some value" etc.
Is there any reason to do something instead of using a namespace instead? Are there any advantages / disadvantages of one over the other.
--------- Change ----------
Sorry, I obviously should have clearly indicated this, since no one deduced it from the class name - "Defines". those. these constants are not associated with a particular class, there is a class created specifically to contain constants and nothing else, that is, the whole class that contains Definitions.
, , , - , , ..
( , , , ).
----- 32- -----------
--- const char * :: StringN = "Somevalue"
.h .cpp?
, . , , " " , . . .
, , - . - , Defines :
Defines
template<typename T> int function() { return T::x + T::y; } //later cout << function<Defines>() << function<OtherDefines>() << endl;
, , , , , , "", . .
. , , . . , , " " .
. , , .
. IE , . , , , - , , .
, ; , class Defines { public: ... }; struct Defines { ... };. , , , , / , , "", .
class Defines { public: ... };
struct Defines { ... };
, , /. , - , using Defines::constant; .
using Defines::constant;
: , , , . , , .
: const char* Defines::StringN = "Somevalue"; , , . , extern .cpp, , .
const char* Defines::StringN = "Somevalue";
extern
:
NAME, ? , , .
NAME
, . . . .
. , .
If you see String1in a method, you do not know where it came from. If you see Defines::String1, you can say "OK, this is a variable from the class Defines, let me go there and see what it is and what it should be." Looking in one class is much better than looking at the whole namespace, which can even be distributed in several source files. Obviously, if a variable is in a class, because it is used mainly in this class, there is no doubt that where it should be .: D
String1
Defines::String1