Pages

Ads 468x60px

Labels

Friday, March 16, 2012

Sự khác nhau giữa iostream vs iostream.h

Đó là do ngày xưa khi C++ mới ra đời thì tất cả các file của thư viện chuẩn (Standard Runtime Library) đều có đuôi .h nhưng sau này tổ chức ANSI chuẩn hóa lại, và đưa tất cả các hàm của thư viện này vào namespace std , tuy nhiên khi làm như thế thì các chương trình ngày xưa sẽ không dùng được nữa .

Thế nên các header file mới ra đời nhưng không có đuôi .h, chứa đầy đủ các hàm hỗ trợ, và nằm trong namespace std .

Chú ý rằng header file do chúng ta tạo ra luôn có đuôi .h , và rất nhiều header file trong thư viện chuẩn cũng có đuôi .h .

When C++ was first created, all of the files in the standard runtime library ended in .h. Life was consistent, and it was good. The original version of cout and cin lived in iostream.h. When the language was standardized by the ANSI committee, they decided to move all of the functions in the runtime library into the std namespace (which is generally a good idea). However, this presented a problem: if they moved all the functions into the std namespace, none of the old programs would work any more!
To try to get around this issue and provide backwards compatibility for older programs, a new set of header files was introduced that use the same names but lack the .h extension. These new header files have all their functionality inside the std namespace. This way, older programs that include#include <iosteam.h> do not need to be rewritten, and newer programs can #include <iostream>.
Make sure when you include a header file from the standard library that you use the non .h version if it exists. Otherwise you will be using a deprecated version of the header that is no longer supported.
As a side note, many headers in the standard library do not have a non .h version, only a .h version. For these files, it is fine to include the .h version. Many of these libraries are backwards compatible with standard C programming, and C does not support namespaces. Consequently, the functionality of these libraries will not be accessed through the std namespace. Also, when you write your own header files, they will all have a .h extension, since you will not be putting your code in the std namespace.
Rule: use the non .h version of a library if it exists, and access the functionality through the std namespace. If the non .h version does not exist, or you are creating your own headers, use the .h version
Source : http://www.learncpp.com/cpp-tutorial/19-header-files/
 

Sample text

Sample Text

Sample Text

Everyday, I wake up asking my-self : what is the purpose of Life ?

 
Blogger Templates