Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I put some code into multiple namespaces without duplicating this code?

Assume I have the method defined in the two different namespaces:

namespace foo
{
    void print()
    {
        //do work...
    }
}

namespace bar
{
    void print()
    {
        //do work...
    }
}

The foo::print() and the bar::print() functions are absolutely equal. My project uses the numerous calls of these functions.

Is there a way to remove one of the print() definitions without changing the calls of these function? I mean something like the following (of course, C++ language doesn't allow this construction, it's just an example):

namespace foo, bar  //wrong code!
{
    void print()
    {
        //do work...
    }
}

If there is no way to refactor the code as I want, please tell me, do you like the following decision? Will you be glad if your project contains such code? :)

namespace foo
{
    void print()
    {
        //do work...
    }
}

namespace bar
{
    void print()
    {
        foo::print();
    }
}

ADD:

Thank you guys, I'm fully satisfied by your answers. Just one moment I want you to clarify: is there a difference between using ::foo::print and using foo::print?

like image 641
yakov Avatar asked Dec 30 '13 09:12

yakov


People also ask

Can you use more than one namespace?

You can have the same name defined in two different namespaces, but if that is true, then you can only use one of those namespaces at a time. However, this does not mean you cannot use the two namespace in the same program. You can use them each at different times in the same program.

Can you have 2 namespaces in C#?

Two classes with the same name can be created inside 2 different namespaces in a single program. Inside a namespace, no two classes can have the same name. In C#, the full name of the class starts from its namespace name followed by dot(.)

Can you use multiple namespaces C++?

Namespace is a feature added in C++ and is not present in C. A namespace is a declarative region that provides a scope to the identifiers (names of functions, variables or other user-defined data types) inside it. Multiple namespace blocks with the same name are allowed.

What is the advantages of having more than one namespace?

You can have multiple namespaces inside a single Kubernetes cluster, and they are all logically isolated from each other. They can help you and your teams with organization, security, and even performance!

How do I combine multiple namespaces into one file?

It is strongly discouraged as a coding practice to combine multiple namespaces into the same file. The primary use case is to combine multiple PHP scripts into the same file. To combine global non-namespaced code with namespaced code, only bracketed syntax is supported.

How to combine global non-namespaced code with namespaced code?

To combine global non-namespaced code with namespaced code, only bracketed syntax is supported. Global code should be encased in a namespace statement with no namespace name as in: Example #3 Declaring multiple namespaces and unnamespaced code.

How do you declare multiple namespaces?

A namespace can be declared in multiple blocks in a single file, and in multiple files. The compiler joins the parts together during preprocessing and the resulting namespace contains all the members declared in all the parts.

What is the use of a namespace?

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. All identifiers at namespace scope are visible to one another without qualification.


1 Answers

You can achieve this with a using declaration.

namespace foo
{
    void print()
    {
        //do work...
    }
}

namespace bar
{
    using foo::print;
}

EDIT

Regarding the difference between ::foo::print and foo::print: prepending a qualified name with :: means that you explicitly refer to the one in the global namespace. This can be used to select the global one, even if there is another item with the same name closer in scope.

like image 165
Agentlien Avatar answered Sep 20 '22 13:09

Agentlien