MC logo
[^] CSc 220 Home
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Functional Average] [Functional Average II] [Functional Average III] [Default Parameters] [Function Overloading] [Reference Parameters] [Constant References] [Namespaces] [Namespaces I] [Using Namespaces]
In plain C, all the top-level function names are collected together in the same “bucket.” That can be a problem in a large program, because you have to keep finding new names. For instance, if you are using both a stack and a queue, you might easily wind up with two push methods.

The usual way programmers deal with this is by chosing names like stack_push and queue_push so that collisions are unlikely.

To manage this more cleanly, C++ introduced the namespace, which is just a collection of names for functions (and classes and all the other stuff you name). That is, instead of throwing all the names in one bucket, you can have as many buckets as you need, each called a namespace, and which in turn has a name. We've already encountered the namespace std which contains all the standard library declarations.

To solve the stack/queue problem, you might have a namespace stack, and another queue. You can then use the name push for each function, and write stack::push or queue::push when there's any ambiguity.