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++:
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++:
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