This note is summarized from a code view.
In general, when programming with C, we are used to defining a numeric constant in our code using the macro definition in C:
When developing a project in C + +, there is often a constant usage of lineage C. So, one of the following writing seems to feel somewhat less authentic:
Class MyClass {
static const int my_const = 7;
// ...
};
From the compiler's point of view, both of these are legal, but in the use of a total of the second choice, which is more appropriate? I've heard it before. C-style macro definitions should be used as little as possible in C + +.
After finding some data, it is found that the main reason for reducing the definition of macros in C is as follows:
Macros are extremely easy to make mistakes when defining functions, and it is recommended that you use inline instead of macro functions.
When a macro defines a constant, it performs text substitution during the preprocessing process, and the corresponding symbol is not generated during compilation, which is not conducive to debugging. Therefore, in C + + it is recommended to use a const or an enum to define constants.
There are two ways to define a constant in C + +, one of which is to use the static const, and the other is to use an enum. Like what:
Class MyClass {
static const int my_const = 7;
enum {another_const = 7};
// ...
};
In both ways, there are some small names in it. When you define a constant using const in a class, you must use static to decorate the constant, and you need to declare the constant outside of the class definition. This is not necessary for an enum, so Bjane Stroustrup suggests using an enum in the FAQ he maintains to define constants.
There are a number of ways to define In-class constants in C + +, and it is possible to define the three defined methods mentioned above. When writing a private project, of course, look at your personal preferences, which one you want to use; it is preferable to follow a team-initiated code specification as part of a development team. Here personally biased enum this definition method, at the same time I will choose to capitalize the constants all:
Class MyClass {
enum {my_const = 7};
// ...
};
We often define constants in a common header file, and define constant methods as follows:
Method 1
Commdef.h (Public header file):
const int constname = XXX;
Join #include "commdef.h" where you use this variable
Method 2
Commdef.h (Public header file):
extern const int Constname;
Commdef.cpp file:
const int constname = XXX;
Join #include "commdef.h" where you use this variable
Since both of these methods are compiled and run, programmers rarely pay attention to their differences. Comparison of two methods:
If you add a Delete constant constant, you can use Method 1 more easily, just modify it in the. h file, and if you change the constant value, the program using Method 2 will save the compile time because you don't need to change the header file.
What better way to look at memory usage? Next Test to see.
The test program defines two modules, Test1 and Test2,test1.cpp and Test2.cpp both refer to the Commdef.h header file and use global constants, and we determine whether memory is allocated separately by looking at the constant address of each module.
Environment: Windows + vs2005
//commdef.h file #ifndef lx_commdef_h #define LX_COMMDEF_H const int max_length = 1024; ex
Tern const int min_length;
#endif//commdef.cpp file #include "commdef.h" const int min_length = 10;
Test1.cpp file (Note: header file contents are omitted because of simplicity) #include "commdef.h" #include "test1.h" #include <iostream> using namespace std; void FuncTest1 () {cout << "max_length = << max_length <<", Address: "<< &max_length <
< Endl;
cout << "min_length = << min_length <<", Address: "<< &min_length << Endl;}
Test2.cpp file (Note: header file contents are omitted because of simplicity) #include "commdef.h" #include "test1.h" #include <iostream> using namespace std; void FuncTest2 () {cout << "max_length = << max_length <<", Address: "<< &max_length
;< Endl;
cout << "min_length = << min_length <<", Address: "<< &min_length << Endl;}
Output:
Max_length = 1024, address:00437ae4
Min_length = ten, address:00437b54
Max_length = 1024, address:00437b1c
Min_length = ten, address:00437b54
As you can see, constants defined with Method 1 are stored separately in multiple modules, and constants defined with Method 2 are stored in one place, so Method 2 is better than Method 1 on storage.
Especially when there are many constants, the header file is referenced in many cases, the improper definition will lead to unnecessary memory waste.
Summarize:
1. No specified type
#define不涉及为定义的常量做类型检查, to explicitly specify a constant type, you need to add a suffix to the constant. For example, for float-type constants, add the F suffix after the number.
2. No scope specified
#define定义的常量是全局的.
3. No access control
The constants defined by #define cannot be marked as public, protected, or private, which is essentially public.
Because once a macro is defined, it works in the subsequent compilation (unless it is #undef somewhere).
4. No sign
In the previous example, the macro max_num_size might be stripped from the code by the preprocessor so that the compiler would not be able to see the name. In this way, the programmer can only see some constant values that are not descriptive when debugging.