Here if I rename the namespace I must also remember to look at the rest of the code, and do all necessary changes. Is there any word like self
, this
or current
to refer to current namespace?
namespace myNamespace {
int myInt;
void myFunc() {
myNamespace::myInt = 66;
}
};
Typically, you declare a namespace in a header file. If your function implementations are in a separate file, then qualify the function names, as in this example. A namespace can be declared in multiple blocks in a single file, and in multiple files.
C++ allows us to define our own namespaces via the namespace keyword. Namespaces that you create for your own declarations are called user-defined namespaces. Namespaces provided by C++ (such as the global namespace ) or by libraries (such as namespace std ) are not considered user-defined namespaces.
A Namespace is a set of unique names. Namespace is a mechanisms by which element and attribute name can be assigned to a group. The Namespace is identified by URI(Uniform Resource Identifiers).
In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory. As a rule, names in a namespace cannot have more than one meaning; that is, different meanings cannot share the same name in the same namespace.
Why are you reffering to your current namespace? there is no need to for that, you can easily write:
namespace myNamespace { int myInt = 33; void myFunc() { myInt = 33; } };
No matter what variables are declared out there (such as a global myInt
), variables of this scope will be used by default.
However if you want to rename, you can use IDE techniques like replace or refactor.
No, there isn't, but if you really want to do something like that you could fake it.
namespace myNamespace { namespace _ns = ::myNamespace; int myInt; void myFunc() { _ns::myInt = 66; } }
If you change myNamespace
, you need only change _ns
to match it.
Also, the semicolon following the namespace is superfluous.
Good question. No, there is nothing like this.
Furthermore, pay attention that myNamespace::myInt
isn’t even unambiguous in the case of nested namespaces (say you’ve got the convoluted case of namespace myNamespace { namespace myNamespace { … } }
. To be on the safe side you need to use the fully qualified namespace:
::myNamespace::myInt = 33;
Where the prefixed ::
refers to the global namespace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With