tailieunhanh - A Complete Guide to Programming in C++ part 24
A Complete Guide to Programming in C++ part 24. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | NAMESPACES 209 Using global names in large-scale software projects can lead to conflicts especially when multiple class libraries are in operation. C provides for the use of namespaces in order to avoid naming conflicts with global identifiers. Within a namespace you can use identifiers without needing to check whether they have been defined previously in an area outside of the namespace. Thus the global scope is subdivided into isolated parts. A normal namespace is identified by a name preceded by the namespace keyword. The elements that belong to the namespace are then declared within braces. Example namespace myLib int count double calculate double int . . . This example defines the namespace myLib that contains the variable count and the function calculated . Elements belonging to a namespace can be referenced directly by name within the namespace. If you need to reference an element from outside of the namespace you must additionally supply the namespace. To do so place the scope resolution operator before the element name. Example myLib count 7 Outside of myLib This allows you to distinguish between identical names in different namespaces. You can also use the scope resolution operator to reference global names that is names declared outside of any namespaces. To do so simply omit the name of the namespace. This technique is useful when you need to access a global name that is hidden by an identical name defined in the current namespace. Example demo Not belonging to any namespace Be aware of the following when using namespaces namespaces do not need to be defined contiguously. You can reopen and expand a namespace you defined previously at any point in the program namespaces can be nested that is you can define a namespace within another namespace. Global identifiers belonging to the C standard library automatically belong to the standard namespace std. 210 CHAPTER 1 1 ST0RAGE CLASSES AND NAMESPACES THE KEYWORD using Sample program Demonstrates .
đang nạp các trang xem trước