Why do most of the current standard header files have .h omitted. Do these headers take up less memory or something? Why are they more efficient?
Why do most of the current standard header files have .h omitted. Do these headers take up less memory or something? Why are they more efficient?
It has nothing to do with efficiency, and everything about standards.
Pre-1998, the C++ langauge was very different. In 1998, a new ANSI C++ standard was published and the language changed. The standard library changed, too, so the headers changed.
To prevent ambiguity, the names were also changed. The .h extentions were dropped and C standard libraries gained a "c" as a prefix. E.g. the new version (post-1998) of <iostream.h> is <iostream> and the new version of <stdlib.h> is <cstdlib>.
The most obvious change is namespaces -- the new headers put their variables, classes, and functions in the std namespace; the old put them in the global namespace.
Last edited by Cat; 06-12-2003 at 04:07 PM.
I see.
But why the change in standard? In 1998, why were new standard libraries created?
For a number of reasons.
1) Namespaces make libraries much easier. For example, say I make a function called less_than, and include it in a library. It would be unrealistic of me to expect that nobody else will name something the same. Namespaces let us both have our way -- as long as we use different namespaces, we can each have a function called less_than.
This helps a LOT when (like me), you have many libraries installed. For example, I might use std::auto_ptr for one task (part of the C++ standard library), boost::shared_ptr for another task (from the Boost library), and I never have to worry about name conflicts. Boost's stuff is in one namespace, the standard library's is in another, Blitz++'s stuff is in another, etc.
2) They added many new features. The STL had been very widely used because of its many useful features; these are now officially part of the C++ language. This means I can use code with vectors, maps, etc. and still have portability; any ANSI-C++ compiler can handle them. E.g., say I want a dynamically sized array (happens in LOTS of code). Unfortunately, it's also prone to memory leaks. Now, I can use vector to accomplish the same thing:
vector<MyObject> objArray(size);
replaces
MyObject * objArray = new MyObject[size];
and is much safer to use (it requires no work at all on my behalf to ensure it won't leak memory or try to free the same memory twice).
3) They redid many of the classes to use templates. For example, basic_string<> is a template, used in string and wstring, and it makes it easy to develop applications that use other character sets, like Unicode (which I use exclusively).
4) They made new classes to supercede some non-robust older classes. E.g. the strstream classes are superceded by stringstream classes.
Last edited by Cat; 06-12-2003 at 04:22 PM.