• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Question Am I getting a grasp of namespace correctly?

Amol S.

Platinum Member
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();
}
 
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
 
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.
 
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.
 
Back
Top