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 .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
12    26    1    30-11-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.