• Guest, The rules for the P & N subforum have been updated to prohibit "ad hominem" or personal attacks against other posters. See the full details in the post "Politics and News Rules & Guidelines."

Question Am I getting a grasp of namespace correctly?

Amol S.

Senior member
Mar 14, 2015
865
122
106
Based on the example bellow, am I getting a grasp of namespace correctly as a means to group related things together, or should namespace in C++ be used in different situations/usage purposes.

C++:
#include <iostream>

using std::cout;

namespace English
{
    void Hello()
    {
        cout << "Hello!  " << "\n";
    }
    void Description()
    {
        cout << "An english speaking person.  " << "\n";
    }
}

namespace Spanish
{
    void Hello()
    {
        cout << "Hola!  "  << "\n";
    }
    void Description()
    {
        cout << "A spanish speaking person.  " << "\n";
    }
}

namespace Trumpish
{
    void Hello()
    {
        cout << "Go back to where you came from illegal!  " << "\n";
    }
    void Description()
    {
        cout << "White person carrying a gun and a red cap that reads \n"
            << "MAKE AMERICA GREAT AGAIN.  " << "\n";
    }
}

int main()
{
    English::Hello();
    English::Description();
    Spanish::Hello();
    Spanish::Description();
    Trumpish::Hello();
    Trumpish::Description();
}
 

YuliApp

Member
Dec 27, 2017
183
44
71
yuliapp.com
no, not really. i mean you could, but why.
namespaces are meant to separate "projects", to group something you would consider relevant and separate on its own. for content above would be one namespace sufficient.
have class which supports hello and description and is initialized with language type. this way you can also recast them
 

Cogman

Lifer
Sep 19, 2000
10,269
104
106
You are using a namespace where you should be using a class and pure virtual class.

Namespaces are much more broad. You'd generally reserve them for cases where you are creating libraries.

The point of namespaces is to make it easy to disambiguate methods and classes. Usually, they don't have to be too fine grained (as Yuli points out, one per project or library is generally good enough).

They are roughly equivelent to Java Packages if that helps, though C++ namespaces tend to be larger.
 

Red Squirrel

No Lifer
May 24, 2003
59,312
8,433
126
www.uovalor.com
To me a namespace is more something you would use to encompass a large group of classes and a major purpose is to ensure you don't end up with conflicting class/function names.

For example you build a "library" of classes that you want to reuse throughout many applications. Let's just say it's a network stack for example. You'll have classes, functions etc with names like Socket, Connect, Connection, Data, IpAddress etc.... these are names that could potentially be used somewhere else depending on what project you're working on and what other libraries you include, and you would end up with conflicts.

To prevent that from happening you'd put all of your classes in a common namespace, call it MyNetworkStack. So when calling those functions if there are situations where they are conflicting you would specify MyNetworkStack::Socket for example.

Normally you don't need to do that if you use "using namespace MyNetworkStack" in the program but in situations where there is conflict you would specify it.

At least, I think that's the main purpose of them.
 

ASK THE COMMUNITY