From: www.codeproject.com
Title: USING NAMESPACES PROPERLY
Author: Dejan Jelovic
Translator: subject to my translation level, it may not be fully accurately expressed, if you have a problem in reading this article, you can write to me write coolgrass@sina.com
Use the correct way to use Namespace
The role of Namespace in C is not in general. The purpose of this paper is not to explain the syntax of the namespace, but to demonstrate how to use the namespace, or a tip using the namespace.
Namespaces can be simply encapsulated with another name package. for example:
Namespace net {
Class socket {
...
}
}
...
NET :: Socket Socket;
After such a package, if the Socket class is implemented in two libraries, as long as their namespace name is different, you can use them without any name conflicts.
But there is still a problem: If two companies have to write a NetWork library, how big is the possibility of using sockets when they write code? I guess is close to 100%.
The namespace name is preferably convenient, that is, the name of the namespace is best not too long, and 2-4 characters can be. Holding such an idea, how big is the two companies called NET in their namespace? 5% or 10%?
It is not difficult to see that the namespace does not solve all problems, it just makes the chance of naming conflicts are relatively small.
There is a method called "Industrial Strength Solution", which uses a long unique name when naming Namespace, and uses a short alias in the program. This NetWork library may be like this:
Namespace net_33843894 {
Class socket {
...
}
}
Nums behind NET_ are generated by a random number generator. For the convenience of the description, we assume that the above code is placed in the
When the user uses our library, we must write his own header file
#include
Namespace net = NET_33843894;
He created an alias in this engineering, used to represent the namespace provided to his library. If the name NET has been used by other libraries, then he can also choose one name instead, such as Net2, Sock, or other.
Do you do everything? not yet. You still have to do something: make your library more simple, more convenient. In this perfect society, people have doubled a installation file, your library should be available in their development environment, next is #include
However, the current situation is that users need to create a head file in order to use your library, although this is not big, but not every user can endure this. The method of solving is to provide a reasonable default value. If the user feels uncomfortable, it can be canceled, so use the precompiled selection option in your header file, as follows:
Namespace net_33843894 {
Class socket {
...
}
}
#ifndef no_net_33843894_ALIAS
Namespace net = NET_33843894;
#ENDIF
In this way, we provide a default value for the name of the namespace. If this name has already been used, then the user can define a NO_NET_33843894_alias macro, the alias will be canceled.
Unfortunately, even if you use a short alias NET, when you use the Socket class, when you use the Socket class, in the compiler I have used, there is no short alias that can be displayed in the error prompt information, but still Use Net_33843894 :: Socket. It is a bit hard to read.
How to do? Look at me.
#ifdef no_net_33843894_ALIAS
Namespace net_33843894 {
#ELSE
Namespace net {
#ENDIF
Class socket {
...
}
}
#ifndef no_net_33843894_ALIAS
Namespace net_33843894 = net;
#ENDIF
If you don't define macro NO_NET_33843894_Alias, you will directly give the namespace a short name, and you can get a long time. In this way, the error message will be more pleasant.