NamespaceGuide.pdf - Using using How to Use the std...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Using “using” - How to Use the std Namespace David Kieras, EECS Department, University of Michigan Febrary 2015 Why Namespaces? When programs get very large and complex, and make heavy use of libraries from a variety of sources, the possibility of name collisions rears its incredibly annoying head. A name collision is when an identifier (say a function name) is used in one part of the code to refer to something, but that same identifier already is being used in a different part of the code to refer to something else. Often the programmer can just use a different name for one of them, but if the two conflicting names are defined in two big expensive librar- ies that are both needed for the project, the programmer is stuck — the compiler won’t let the same name be used for two different things! A good example is a class or struct for representing points in Cartesian space with x, y coordinates. What’s the obvious name for this? “Point”, or perhaps “point”. Where might such a thing be used? In the GUI library, to refer to points on the display, normally as a pair of integers. Also in say a geometry/trigonometry library to refer to points on the plane, normally as a pair of double-precision floating point numbers. Oops! Two definitions of “Point” are now in play. If we want to write code using the trig library to determine lines and shapes to put on the display, we are stuck with using both versions of Point at once, so the name collision can’t be avoided. If we are lucky, we won’t have the problem because one of the library developers used something like CPoint instead of Point, but that is purely a matter of luck. Namespaces can solve the problem in the absence of luck. The Namespace Concept The namespace idea originated in other languages (e.g. LISP). The idea is that identifiers can be grouped into separate sets, each set associated with a particular library or body of code, and each such set, or namespace, itself has a name. Thus if the same names appear in two libraries, each of which are in their own namespace, the collision can be resolved by qualifying the names with the namespace name. For example, if we want a GUI point, we might write GUILib::Point , and we might designate a trig library point with TrigLib::Point . Note how this is using the double colon ( :: ), the s cope qualification operator , analogously to how it is used for class member names. To avoid the inconvenience of writing the namespace name all the time, we might want to specify that an unqualified Point means the Point from TrigLib. Then if we want a GUILib Point, we have to use the qualified GUILib::Poin t name. C++ has “using” statements that provide this convenience. The global namespace is where names reside if you don’t put them into a specific namespace. All of the code you normally write will declare functions whose names are in the global namespace. Such names can almost always be used without qualification, but
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern