Namespaces (::)

namespace::feature()

Namespaces are an easy way to avoid name conflicts in large projects by grouping related code under a name. They help keep your code organized and prevent different parts of the program from interfering with each other. They can look like namespace::feature and might be slightly confusing to newer users.

Here's a simple example of how to use namespaces in C++:

#include <iostream>

// Define a namespace called 'First'
namespace First {
    // Inside this namespace, we define a function 'display'
    void display() {
        std::cout << "This is the First namespace." << std::endl;
    }
}

// Define another namespace called 'Second'
namespace Second {
    // Inside this namespace, we define another function 'display'
    void display() {
        std::cout << "This is the Second namespace." << std::endl;
    }
}

int main() {
    // Call the 'display' function from the 'First' namespace
    First::display();
    
    // Call the 'display' function from the 'Second' namespace
    Second::display();
    
    return 0;
}

In this example, both First and Second namespaces have their own display function. By specifying the namespace (using First:: or Second::), you avoid any confusion between the two functions. This is especially helpful in larger projects where different parts of the code might use the same function names.

PROS uses namespaces extensively with their pros namespace, while VEXCode uses their vex namespace accordingly.

Didn't Understand What was Written Above?

Ok let's use a non-code example:

Your friend asks for you to get a burger. However, you don't know what sort of burger they want.

They could be asking for a burger from Burger King, McDonalds, or some other fast food restuarant!

If we were to treat the menus of these fast food restuarants as namespaces, and the burgers as functions or classes, they'd look something like this in C++:

McDonalds::burger

Burger_King::burger

Now, we can have foods (objects or functions) such as a "burger" from different resturaunts (namespaces).

Teams Contributed to this Article:

  • BLRS (Purdue SIGBots)

Last updated

Logo

This work is licensed under a Attribution-ShareAlike 2.0 Generic License