C developers often use # define, that is, macro to declare constants, but macros are global, which is difficult to maintain for large projects and often leads to name conflicts. Fortunately, C ++ brings us the namespace. It is used as follows. A namespace can group a group of logic, and the namespace is also a scope.
View plain
Copy to clipboard
Print
?
- Namespace
Outspname
- {
- Const
Int
Cvar1 = 1;
- Const
Char
*
Const
Cvar2 =
"33333"
;
- Void
Test ();
- Namespace
Inspname
- {
- Enum
{A, B, c };
- Class
Klass
- {
- };
- }
- }
Namespace outspname <br/>{< br/> const int cvar1 = 1; <br/> const char * const cvar2 = "33333"; <br/> void test (); </P> <p> namespace inspname <br/>{< br/> Enum {a, B, c }; <br/> class Klass <br/>{< br/>}; <br/>}< br/>}
However, even a simple namespace has many xuanjicang.
1. When a name is used out of its own space, it is repeatedly prefixed with a namespace, such
View plain
Copy to clipboard
Print
?
- Const
Int
Local = outspname: inspname:
Const int local = outspname: inspname:
This is not annoying. InWithin a small local scope,
We can use a declaration. For example
View plain
Copy to clipboard
Print
?
- {
- Using
Outspname: inspname:;
- Const
Int
Local =;
- }
{<Br/> using outspname: inspname: A; <br/> const int local = A; <br/>}
2. Also, we can use a command to change all the names in the namespace to available. As shown below, there is a namespace behind using.Similarly, only using namesapce is used in a small local scope. Otherwise, the name will be contaminated.
View plain
Copy to clipboard
Print
?
- {
- Using
Namespace
Outspname;
- Const
Int
Local2 = cvar1;
- Const
Int
Local2 = inspname: B;
- {
- Using
Namespace
Inspname;
- Klass * P = new
Klass ();
- }
- }
{<Br/> using namespace outspname; <br/> const int local2 = cvar1; <br/> const int local2 = inspname: B; <br/>{< br/> using namespace inspname; <br/> Klass * P = new Klass (); <br/>}</P> <p>}
However, when using namespace, pay attention to the following. For example, the testname: test method is declared in a. h file.
View plain
Copy to clipboard
Print
?
- Namespace
Testname
- {
- Void
Test (
Int
Param );
- }
Namespace testname <br/>{< br/> void test (int param); <br/>}
In its. cpp,The following method cannot be used. The test method is only a local method of the compilation unit, rather than the test method of the testname namespace.
View plain
Copy to clipboard
Print
?
- Using
Testname;
- Void
Test (
Int
Param)
- {
- }
Using testname; <br/> void test (int param) <br/>{< br/>}
The correct method is
View plain
Copy to clipboard
Print
?
- Namespace
Testname
- {
- Void
Test (
Int
Param)
- {
- }
- }
Namespace testname <br/>{< br/> void test (int param) <br/>{< br/>}< br/>}
Or
View plain
Copy to clipboard
Print
?
- Void
Testname: Test (
Int
Param)
- {
- }
Void testname: Test (int param) <br/>{< br/>}
3. The namespace alias. When the namespace is very long or nested, we can use the namespace alias as follows:
View plain
Copy to clipboard
Print
?
- Namespace
Oin = outspname: inspname;
Namespace oin = outspname: inspname;
4. An unnamed namespace is mainly used to preserve the code locality. The usage is as follows:
View plain
Copy to clipboard
Print
?
- Namespace
- {
- Const
Int
Cvar1 = 1;
- Void
Test ();
- }
Namespace <br/>{< br/> const int cvar1 = 1; <br/> void test (); <br/>}
However, you must note that,In the implementation of the C ++ compiler, the unnamed namespace actually has a name. This implicit name is related to the name of the compilation unit in which it is located.
Therefore, we cannot use the name in the namespace across compilation units.
The above statement is equivalent
View plain
Copy to clipboard
Print
?
- Namespace
$
- {
- Const
Int
Cvar1 = 1;
- Void
Test ();
- }
- Using
Namespace
$;
Namespace $ <br/>{< br/> const int cvar1 = 1; <br/> void test (); <br/>}< br/> using namespace $;
Among them, $ has unique names in the scope where it is located, and the Unnamed namespaces in each compilation unit are also different, using namesapce $ is only the implicit name of the current compilation unit, soThe name in the namespace cannot be used across compilation units.
Assume that the test method is defined and implemented in A. H and A. cpp, but the test method or cvar1 cannot be used directly in B. H or B. cpp. In this compilation unit of B, the test symbol in the compilation unit of B is linked. It is not the test symbol in the compilation unit of A, and there will be uncertain symbols.
5. Avoid using a very short name in the namespace. It cannot be too long or nested too deeply. I personally think it should not exceed four layers.
Reprinted with the source: http://blog.csdn.net/szlanny/archive/2009/07/12/4341531.aspx