Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I refer the current namespace while declaring it? Is there any word like "self"/"this"?

Tags:

c++

namespaces

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;
  }
};
like image 560
rsk82 Avatar asked Dec 25 '11 15:12

rsk82


People also ask

How do you declare namespace?

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.

Can we define our own namespace?

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.

What are namespaces and how they are declared?

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).

What is namespace give the example?

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.


3 Answers

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.

like image 126
Tamer Shlash Avatar answered Oct 27 '22 07:10

Tamer Shlash


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.

like image 30
Ferruccio Avatar answered Oct 27 '22 08:10

Ferruccio


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.

like image 42
Konrad Rudolph Avatar answered Oct 27 '22 07:10

Konrad Rudolph