namesp

namesp - C+ Namespaces INTRODUCTION TO NAMESPACES - PART 1...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
C++ Namespaces INTRODUCTION TO NAMESPACES - PART 1 Namespaces are a relatively new C++ feature just now starting to appear in C++ compilers. We will be describing some aspects of namespaces in subsequent newsletters. What problem do namespaces solve? Well, suppose that you buy two different general-purpose class libraries from two different vendors, and each library has some features that you'd like to use. You include the headers for each class library: #include "vendor1.h" #include "vendor2.h" and then it turns out that the headers have this in them: // vendor1.h ... various stuff . .. class String { ... }; // vendor2.h ... various stuff . .. class String { ... }; This usage will trigger a compiler error, because class String is defined twice. In other words, each vendor has included a String class in the class library, leading to a compile-time clash. Even if you could somehow get around this compile-time problem, there is the further problem of link-time clashes, where two libraries contain some identically-named symbols. The namespace feature gets around this difficulty by means of separate named namespaces: // vendor1.h ... various stuff . .. namespace Vendor1 { class String { ... }; } // vendor2.h ... various stuff . .. namespace Vendor2 {
Background image of page 1

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

View Full DocumentRight Arrow Icon
class String { ... }; } There are no longer two classes named String, but instead there are now classes named Vendor1::String and Vendor2::String. In future discussions we will see how namespaces can be used in applications. INTRODUCTION TO NAMESPACES - PART 2
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

namesp - C+ Namespaces INTRODUCTION TO NAMESPACES - PART 1...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online