Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested NameSpaces in C++

Tags:

c++

namespaces

I am confused what to do when having nested namespaces and declarations of objects.

I am porting some code that links against a static library that has a few namespaces.

Example of what I am talking about:

namespace ABC {

    namespace XYZ {

        //STUFF
    }
}

In code what do I do to declare an object that is in namespace XYZ?

if I try:

XYZ::ClassA myobject;

or:

ABC::XYZ::ClassA myobject;

or:

ABC::ClassA myobject;

I get

does not name a type

errors, even though ClassA definitely exists.

What is proper here?

like image 657
jDOG Avatar asked Jul 07 '10 21:07

jDOG


People also ask

What is a nested namespace?

A namespace inside a namespace is called a nested namespace in C#. This is mainly done to properly structure your code. We have an outer namespace − namespace outer {} Within that, we have an inner namespace inside the outer namespace − namespace inner { public class innerClass { public void display() { Console.

Can we have nested namespace in C++?

In C++, namespaces can be nested, and resolution of namespace variables is hierarchical. For example, in the following code, namespace inner is created inside namespace outer, which is inside the global namespace.

What is the use of nested namespace in C#?

When a Namespace is declared inside the other namespace that declaration is called nesting namespaces. When a Namespace is declared inside the other namespace that declaration is called nesting namespaces. You can nest namespaces to any level you want.

What is a namespace in C?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.


2 Answers

It depends on the namespace you already are:

If you're in no namespace or another, unrelated namespace, then you have to specify to whole path ABC::XYZ::ClassA.

If you're in ABC you can skip the ABC and just write XYZ::ClassA.

Also, worth mentioning that if you want to refer to a function which is not in a namespace (or the "root" namespace), you can prefix it by :::

Example:

int foo() { return 1; }

namespace ABC
{
  double foo() { return 2.0; }

  void bar()
  {
    foo(); //calls the double version
    ::foo(); //calls the int version
  }
}
like image 168
ereOn Avatar answered Sep 29 '22 06:09

ereOn


If myobject is declared in that namespace and you want to declare it again (for defining it), you do it by prefixing its name, not its type.

ClassA ABC::XYZ::myobject;

If its type is declared in that namespace too, you also need to prefix the name of the type

ABC::XYZ::ClassA ABC::XYZ::myobject;

It's rarely needed to redeclare an object like that. Often the first declaration of an object is also its definition. If you want to first declare the object, you have to do it in that namespace. The following declares and defines "myobject"

namespace ABC {
  namespace XYZ {
    ClassA myobject;
  }
}

If you have defined in object like this, you refer to it by saying ABC::XYZ. You don't have to "declare" that object somehow in order to use it locally

void f() {
  ABC::XYZ::myobject = someValue;

  // you *can* however use a using-declaration
  using ABC::XYZ::myobject;
  myobject = someValue;
}
like image 28
Johannes Schaub - litb Avatar answered Sep 29 '22 05:09

Johannes Schaub - litb